diff --git a/.gitignore b/.gitignore index 1d7e1191..a6a53467 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ test/fixtures/ts/node_modules/aliyun-egg/ !test/fixtures/example-ts-simple/node_modules/ !test/fixtures/test-files/node_modules/ !test/fixtures/test-demo-app/node_modules/ +!test/fixtures/test-demo-app-esm/node_modules/ .mochawesome-reports run diff --git a/package.json b/package.json index b0ece7e7..2e3b7f36 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "node": ">= 18.19.0" }, "dependencies": { - "@eggjs/utils": "^4.1.2", + "@eggjs/utils": "^4.2.0", "@oclif/core": "^4.2.0", "@types/mocha": "^10.0.10", "@types/supertest": "^6.0.2", @@ -35,7 +35,7 @@ "utility": "^2.4.0" }, "peerDependencies": { - "@eggjs/mock": "beta" + "@eggjs/mock": "6" }, "peerDependenciesMeta": { "@eggjs/mock": { @@ -44,7 +44,7 @@ }, "devDependencies": { "@arethetypeswrong/cli": "^0.17.1", - "@eggjs/mock": "beta", + "@eggjs/mock": "6", "@eggjs/tsconfig": "1", "@swc-node/register": "^1.6.1", "@swc/core": "^1.3.35", diff --git a/src/commands/test.ts b/src/commands/test.ts index 10b167ca..6bb712a8 100644 --- a/src/commands/test.ts +++ b/src/commands/test.ts @@ -4,7 +4,7 @@ import os from 'node:os'; import fs from 'node:fs/promises'; import { Args, Flags } from '@oclif/core'; import globby from 'globby'; -import { importResolve } from '@eggjs/utils'; +import { importResolve, detectType, EggType } from '@eggjs/utils'; import { getChangedFilesForRoots } from 'jest-changed-files'; import { BaseCommand } from '../baseCommand.js'; @@ -115,15 +115,19 @@ export default class Test extends BaseCommand { const { args, flags } = this; // collect require const requires = await this.formatRequires(); - // try { - // const eggMockRegister = importResolve('@eggjs/mock/register', { paths: [ this.base ] }); - // requires.push(eggMockRegister); - // debug('auto register @eggjs/mock/register: %o', eggMockRegister); - // } catch (err) { - // // ignore @eggjs/mock not exists - // debug('auto register @eggjs/mock fail, can not require @eggjs/mock on %o, error: %s', - // this.base, (err as Error).message); - // } + const eggType = await detectType(flags.base); + debug('eggType: %s', eggType); + if (eggType === EggType.application) { + try { + const eggMockRegister = importResolve('@eggjs/mock/register', { paths: [ flags.base ] }); + requires.push(eggMockRegister); + debug('auto register @eggjs/mock/register: %o', eggMockRegister); + } catch (err: any) { + // ignore @eggjs/mock not exists + debug('auto register @eggjs/mock fail, can not require @eggjs/mock on %o, error: %s', + flags.base, err.message); + } + } // handle mochawesome enable let reporter = this.env.TEST_REPORTER; diff --git a/test/commands/test.test.ts b/test/commands/test.test.ts index 3e469d09..85e0e48f 100644 --- a/test/commands/test.test.ts +++ b/test/commands/test.test.ts @@ -20,6 +20,30 @@ describe('test/commands/test.test.ts', () => { .end(); }); + it('should work on auto require @eggjs/mock/register on CommonJS', () => { + if (process.platform === 'win32') return; + return coffee.fork(eggBin, [ 'test' ], { + cwd: getFixtures('test-demo-app'), + }) + .debug() + .expect('stdout', /should work/) + .expect('stdout', /a\.test\.js/) + .expect('code', 0) + .end(); + }); + + it('should work on auto require @eggjs/mock/register on ESM', () => { + if (process.platform === 'win32') return; + return coffee.fork(eggBin, [ 'test' ], { + cwd: getFixtures('test-demo-app-esm'), + }) + .debug() + .expect('stdout', /should work/) + .expect('stdout', /a\.test\.js/) + .expect('code', 0) + .end(); + }); + it('should success when no changed files', () => { return coffee.fork(eggBin, [ 'test', '-c' ], { cwd }) // .debug() diff --git a/test/fixtures/test-demo-app-esm/app/router.js b/test/fixtures/test-demo-app-esm/app/router.js new file mode 100644 index 00000000..7637f409 --- /dev/null +++ b/test/fixtures/test-demo-app-esm/app/router.js @@ -0,0 +1,8 @@ +export default app => { + app.get('/', async function() { + this.body = { + fooPlugin: app.fooPlugin, + foo: 'bar', + }; + }); +}; diff --git a/test/fixtures/test-demo-app-esm/config/config.default.js b/test/fixtures/test-demo-app-esm/config/config.default.js new file mode 100644 index 00000000..a0924020 --- /dev/null +++ b/test/fixtures/test-demo-app-esm/config/config.default.js @@ -0,0 +1,3 @@ +export default { + keys: '123', +}; diff --git a/test/fixtures/test-demo-app-esm/node_modules/egg b/test/fixtures/test-demo-app-esm/node_modules/egg new file mode 120000 index 00000000..56f30152 --- /dev/null +++ b/test/fixtures/test-demo-app-esm/node_modules/egg @@ -0,0 +1 @@ +../../../../node_modules/egg \ No newline at end of file diff --git a/test/fixtures/test-demo-app-esm/package.json b/test/fixtures/test-demo-app-esm/package.json new file mode 100644 index 00000000..3f0e4d48 --- /dev/null +++ b/test/fixtures/test-demo-app-esm/package.json @@ -0,0 +1,4 @@ +{ + "name": "demo-app-esm", + "type": "module" +} diff --git a/test/fixtures/test-demo-app-esm/test/a.test.js b/test/fixtures/test-demo-app-esm/test/a.test.js new file mode 100644 index 00000000..1ffc212e --- /dev/null +++ b/test/fixtures/test-demo-app-esm/test/a.test.js @@ -0,0 +1,10 @@ +import { app } from '@eggjs/mock/bootstrap'; + +describe('a.test.js', () => { + it('should work', async () => { + await app.httpRequest() + .get('/') + .expect(200) + .expect({ foo: 'bar' }); + }); +}); diff --git a/test/fixtures/test-demo-app/app/router.js b/test/fixtures/test-demo-app/app/router.js index d2bcd052..609c03b3 100644 --- a/test/fixtures/test-demo-app/app/router.js +++ b/test/fixtures/test-demo-app/app/router.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = function(app) { app.get('/', async function() { this.body = { diff --git a/test/fixtures/test-demo-app/test/a.test.js b/test/fixtures/test-demo-app/test/a.test.js index b9a8919f..5129c954 100644 --- a/test/fixtures/test-demo-app/test/a.test.js +++ b/test/fixtures/test-demo-app/test/a.test.js @@ -2,7 +2,6 @@ const { app } = require('@eggjs/mock/bootstrap'); describe('a.test.js', () => { it('should work', async () => { - await app.ready(); await app.httpRequest() .get('/') .expect(200)