From 54a036efc57f296b965a49449abec65c953553a9 Mon Sep 17 00:00:00 2001 From: angleshe <478647464@qq.com> Date: Wed, 1 Jan 2020 12:51:44 +0800 Subject: [PATCH 1/2] fix(test): auto add .setup.ts file --- lib/cmd/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cmd/test.js b/lib/cmd/test.js index d7694d8b..47e9f8aa 100644 --- a/lib/cmd/test.js +++ b/lib/cmd/test.js @@ -163,7 +163,7 @@ class TestCommand extends Command { } // auto add setup file as the first test file - const setupFile = path.join(process.cwd(), 'test/.setup.js'); + const setupFile = path.join(process.cwd(), `test/.setup.${testArgv.typescript ? 'ts' : 'js'}`); if (fs.existsSync(setupFile)) { files.unshift(setupFile); } From 4169464955ffb2fc20cf1b4002464d6932f73bcf Mon Sep 17 00:00:00 2001 From: angleshe <478647464@qq.com> Date: Thu, 2 Jan 2020 12:28:39 +0800 Subject: [PATCH 2/2] add unit test --- test/fixtures/setup-ts/package.json | 6 ++++++ test/fixtures/setup-ts/test/.setup.ts | 6 ++++++ test/fixtures/setup-ts/test/a.test.ts | 7 +++++++ test/fixtures/setup-ts/tsconfig.json | 19 +++++++++++++++++++ test/lib/cmd/test.test.js | 12 ++++++++++++ 5 files changed, 50 insertions(+) create mode 100644 test/fixtures/setup-ts/package.json create mode 100644 test/fixtures/setup-ts/test/.setup.ts create mode 100644 test/fixtures/setup-ts/test/a.test.ts create mode 100644 test/fixtures/setup-ts/tsconfig.json diff --git a/test/fixtures/setup-ts/package.json b/test/fixtures/setup-ts/package.json new file mode 100644 index 00000000..3cb71fd5 --- /dev/null +++ b/test/fixtures/setup-ts/package.json @@ -0,0 +1,6 @@ +{ + "name": "ts", + "egg": { + "framework": "aliyun-egg" + } +} \ No newline at end of file diff --git a/test/fixtures/setup-ts/test/.setup.ts b/test/fixtures/setup-ts/test/.setup.ts new file mode 100644 index 00000000..1a6859a8 --- /dev/null +++ b/test/fixtures/setup-ts/test/.setup.ts @@ -0,0 +1,6 @@ +before(() => { + console.log('this is a before function'); +}); +afterEach(() => { + console.log('is end!'); +}) \ No newline at end of file diff --git a/test/fixtures/setup-ts/test/a.test.ts b/test/fixtures/setup-ts/test/a.test.ts new file mode 100644 index 00000000..4addb24e --- /dev/null +++ b/test/fixtures/setup-ts/test/a.test.ts @@ -0,0 +1,7 @@ +'use strict'; + +describe('a.test.ts', () => { + it('test', () => { + console.log('hello egg'); + }); +}); diff --git a/test/fixtures/setup-ts/tsconfig.json b/test/fixtures/setup-ts/tsconfig.json new file mode 100644 index 00000000..4f10abf7 --- /dev/null +++ b/test/fixtures/setup-ts/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "target": "es2017", + "module": "commonjs", + "strict": true, + "noImplicitAny": false, + "moduleResolution": "node", + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "pretty": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true, + "skipLibCheck": true, + "skipDefaultLibCheck": true, + "inlineSourceMap": true, + "importHelpers": true + }, +} \ No newline at end of file diff --git a/test/lib/cmd/test.test.js b/test/lib/cmd/test.test.js index 65389f1c..7a47d781 100644 --- a/test/lib/cmd/test.test.js +++ b/test/lib/cmd/test.test.js @@ -122,6 +122,7 @@ describe('test/lib/cmd/test.test.js', () => { it('should auto require test/.setup.js', () => { // example: https://github.com/lelandrichardson/enzyme-example-mocha + mm(process.env, 'TESTS', 'test/Foo.test.js'); return coffee.fork(eggBin, [ 'test' ], { cwd: path.join(__dirname, '../../fixtures/enzyme-example-mocha') }) // .debug() .expect('stdout', /before hook: delay 10ms/) @@ -130,6 +131,16 @@ describe('test/lib/cmd/test.test.js', () => { .end(); }); + it('should auto require test/.setup.ts', () => { + // example: https://github.com/lelandrichardson/enzyme-example-mocha + mm(process.env, 'TESTS', 'test/a.test.ts'); + return coffee.fork(eggBin, [ 'test', '--typescript' ], { cwd: path.join(__dirname, '../../fixtures/setup-ts') }) + .expect('stdout', /this is a before function/) + .expect('stdout', /hello egg/) + .expect('stdout', /is end!/) + .expect('code', 0) + .end(); + }); it('should force exit', () => { const cwd = path.join(__dirname, '../../fixtures/no-exit'); return coffee.fork(eggBin, [ 'test' ], { cwd }) @@ -140,6 +151,7 @@ describe('test/lib/cmd/test.test.js', () => { it('run not test with dry-run option', () => { const cwd = path.join(__dirname, '../../fixtures/mocha-test'); + mm(process.env, 'TESTS', 'test/foo.test.js'); return coffee.fork(eggBin, [ 'test', '--timeout=12345', '--dry-run' ], { cwd }) .expect('stdout', [ /_mocha/g,