Skip to content

Commit

Permalink
fix: auto set mocha @eggjs/mock/register on application project type (#…
Browse files Browse the repository at this point in the history
…281)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Added support for automatically registering `@eggjs/mock/register` in
test environments
- Introduced ESM (ECMAScript Module) test fixture for enhanced testing
capabilities

- **Bug Fixes**
	- Refined test file handling for changed files
	- Updated dependency versions for compatibility

- **Documentation**
- Added new test cases to verify mock registration in CommonJS and ESM
environments

- **Chores**
- Updated `.gitignore` to allow tracking specific `node_modules`
directory
	- Updated package dependencies and peer dependencies

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Dec 29, 2024
1 parent 87b2aae commit 929c0f8
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -35,7 +35,7 @@
"utility": "^2.4.0"
},
"peerDependencies": {
"@eggjs/mock": "beta"
"@eggjs/mock": "6"
},
"peerDependenciesMeta": {
"@eggjs/mock": {
Expand All @@ -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",
Expand Down
24 changes: 14 additions & 10 deletions src/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -115,15 +115,19 @@ export default class Test<T extends typeof Test> extends BaseCommand<T> {
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;
Expand Down
24 changes: 24 additions & 0 deletions test/commands/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/test-demo-app-esm/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default app => {
app.get('/', async function() {
this.body = {
fooPlugin: app.fooPlugin,
foo: 'bar',
};
});
};
3 changes: 3 additions & 0 deletions test/fixtures/test-demo-app-esm/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
keys: '123',
};
1 change: 1 addition & 0 deletions test/fixtures/test-demo-app-esm/node_modules/egg

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/fixtures/test-demo-app-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "demo-app-esm",
"type": "module"
}
10 changes: 10 additions & 0 deletions test/fixtures/test-demo-app-esm/test/a.test.js
Original file line number Diff line number Diff line change
@@ -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' });
});
});
2 changes: 0 additions & 2 deletions test/fixtures/test-demo-app/app/router.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

module.exports = function(app) {
app.get('/', async function() {
this.body = {
Expand Down
1 change: 0 additions & 1 deletion test/fixtures/test-demo-app/test/a.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 929c0f8

Please sign in to comment.