|
| 1 | +import path from 'node:path'; |
| 2 | +import { fileURLToPath } from 'node:url'; |
| 3 | +import { strict as assert } from 'node:assert'; |
| 4 | +import fs from 'node:fs/promises'; |
| 5 | +import cp from 'node:child_process'; |
| 6 | +import { scheduler } from 'node:timers/promises'; |
| 7 | +import coffee from 'coffee'; |
| 8 | +import { request } from 'urllib'; |
| 9 | +import { mm, restore } from 'mm'; |
| 10 | +import { cleanup, replaceWeakRefMessage, Coffee } from './utils.js'; |
| 11 | +import { isWindows, getSourceFilename } from '../src/helper.js'; |
| 12 | + |
| 13 | +const __filename = fileURLToPath(import.meta.url); |
| 14 | +const __dirname = path.dirname(__filename); |
| 15 | + |
| 16 | +describe('test/ts.test.ts', () => { |
| 17 | + const eggBin = getSourceFilename('../bin/run.js'); |
| 18 | + const homePath = path.join(__dirname, 'fixtures/home'); |
| 19 | + const waitTime = 5000; |
| 20 | + let fixturePath: string; |
| 21 | + |
| 22 | + beforeEach(() => mm(process.env, 'MOCK_HOME_DIR', homePath)); |
| 23 | + afterEach(restore); |
| 24 | + |
| 25 | + before(() => fs.mkdir(homePath, { recursive: true })); |
| 26 | + after(() => fs.rm(homePath, { recursive: true, force: true })); |
| 27 | + |
| 28 | + describe('should display correct stack traces', () => { |
| 29 | + let app: Coffee; |
| 30 | + beforeEach(async () => { |
| 31 | + fixturePath = path.join(__dirname, 'fixtures/ts'); |
| 32 | + await cleanup(fixturePath); |
| 33 | + const result = cp.spawnSync('npm', [ 'run', isWindows ? 'windows-build' : 'build' ], { |
| 34 | + cwd: fixturePath, |
| 35 | + shell: isWindows, |
| 36 | + }); |
| 37 | + assert.equal(result.stderr.toString(), ''); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(async () => { |
| 41 | + app && app.proc.kill('SIGTERM'); |
| 42 | + await cleanup(fixturePath); |
| 43 | + }); |
| 44 | + |
| 45 | + it('--ts', async () => { |
| 46 | + app = coffee.fork(eggBin, [ 'start', '--workers=1', '--ts', fixturePath ]) as Coffee; |
| 47 | + // app.debug(); |
| 48 | + app.expect('code', 0); |
| 49 | + |
| 50 | + await scheduler.wait(waitTime); |
| 51 | + |
| 52 | + assert.equal(replaceWeakRefMessage(app.stderr), ''); |
| 53 | + assert.match(app.stdout, /egg started on http:\/\/127\.0\.0\.1:7001/); |
| 54 | + const result = await request('http://127.0.0.1:7001', { dataType: 'json' }); |
| 55 | + // console.log(result.data); |
| 56 | + assert(result.data.stack.includes(path.normalize('app/controller/home.ts:6:13'))); |
| 57 | + }); |
| 58 | + |
| 59 | + it('--typescript', async () => { |
| 60 | + app = coffee.fork(eggBin, [ 'start', '--workers=1', '--typescript', fixturePath ]) as Coffee; |
| 61 | + // app.debug(); |
| 62 | + app.expect('code', 0); |
| 63 | + |
| 64 | + await scheduler.wait(waitTime); |
| 65 | + |
| 66 | + assert.equal(replaceWeakRefMessage(app.stderr), ''); |
| 67 | + assert.match(app.stdout, /egg started on http:\/\/127\.0\.0\.1:7001/); |
| 68 | + const result = await request('http://127.0.0.1:7001', { dataType: 'json' }); |
| 69 | + // console.log(result.data); |
| 70 | + assert(result.data.stack.includes(path.normalize('app/controller/home.ts:6:13'))); |
| 71 | + }); |
| 72 | + |
| 73 | + it('--sourcemap', async () => { |
| 74 | + app = coffee.fork(eggBin, [ 'start', '--workers=1', '--sourcemap', fixturePath ]) as Coffee; |
| 75 | + // app.debug(); |
| 76 | + app.expect('code', 0); |
| 77 | + |
| 78 | + await scheduler.wait(waitTime); |
| 79 | + |
| 80 | + assert.equal(replaceWeakRefMessage(app.stderr), ''); |
| 81 | + assert.match(app.stdout, /egg started on http:\/\/127\.0\.0\.1:7001/); |
| 82 | + const result = await request('http://127.0.0.1:7001', { dataType: 'json' }); |
| 83 | + // console.log(result.data); |
| 84 | + assert(result.data.stack.includes(path.normalize('app/controller/home.ts:6:13'))); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('pkg.egg.typescript', () => { |
| 89 | + let app: Coffee; |
| 90 | + beforeEach(async () => { |
| 91 | + fixturePath = path.join(__dirname, 'fixtures/ts-pkg'); |
| 92 | + await cleanup(fixturePath); |
| 93 | + const result = cp.spawnSync('npm', [ 'run', isWindows ? 'windows-build' : 'build' ], { |
| 94 | + cwd: fixturePath, |
| 95 | + shell: isWindows, |
| 96 | + }); |
| 97 | + assert(!result.stderr.toString()); |
| 98 | + }); |
| 99 | + |
| 100 | + afterEach(async () => { |
| 101 | + app && app.proc.kill('SIGTERM'); |
| 102 | + await cleanup(fixturePath); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should got correct stack', async () => { |
| 106 | + app = coffee.fork(eggBin, [ 'start', '--workers=1', fixturePath ]) as Coffee; |
| 107 | + // app.debug(); |
| 108 | + app.expect('code', 0); |
| 109 | + |
| 110 | + await scheduler.wait(waitTime); |
| 111 | + |
| 112 | + assert.equal(replaceWeakRefMessage(app.stderr), ''); |
| 113 | + assert.match(app.stdout, /egg started on http:\/\/127\.0\.0\.1:7001/); |
| 114 | + const result = await request('http://127.0.0.1:7001', { dataType: 'json' }); |
| 115 | + // console.log(result.data); |
| 116 | + assert(result.data.stack.includes(path.normalize('app/controller/home.ts:6:13'))); |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
0 commit comments