Skip to content

Commit

Permalink
fix: auto add .setup.ts file (#147)
Browse files Browse the repository at this point in the history
* fix(test): auto add .setup.ts file

* add unit test
  • Loading branch information
angleshe authored and whxaxes committed Jan 2, 2020
1 parent c40f13a commit 00afdf7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/cmd/test.js
Expand Up @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/setup-ts/package.json
@@ -0,0 +1,6 @@
{
"name": "ts",
"egg": {
"framework": "aliyun-egg"
}
}
6 changes: 6 additions & 0 deletions test/fixtures/setup-ts/test/.setup.ts
@@ -0,0 +1,6 @@
before(() => {
console.log('this is a before function');
});
afterEach(() => {
console.log('is end!');
})
7 changes: 7 additions & 0 deletions test/fixtures/setup-ts/test/a.test.ts
@@ -0,0 +1,7 @@
'use strict';

describe('a.test.ts', () => {
it('test', () => {
console.log('hello egg');
});
});
19 changes: 19 additions & 0 deletions 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
},
}
12 changes: 12 additions & 0 deletions test/lib/cmd/test.test.js
Expand Up @@ -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/)
Expand All @@ -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 })
Expand All @@ -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,
Expand Down

0 comments on commit 00afdf7

Please sign in to comment.