Skip to content

Commit df1cc5b

Browse files
whxaxespopomore
authored andcommitted
feat: support jest (#188)
1 parent eb713d9 commit df1cc5b

15 files changed

Lines changed: 157 additions & 10 deletions

File tree

lib/loader/file_loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class FileLoader {
122122
parse() {
123123
let files = this.options.match;
124124
if (!files) {
125-
files = (process.env.EGG_TYPESCRIPT === 'true' && require.extensions['.ts'])
125+
files = (process.env.EGG_TYPESCRIPT === 'true' && utils.extensions.includes('.ts'))
126126
? [ '**/*.(js|ts)', '!**/*.d.ts' ]
127127
: [ '**/*.js' ];
128128
} else {

lib/utils/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,24 @@ const is = require('is-type-of');
55
const path = require('path');
66
const fs = require('fs');
77
const co = require('co');
8+
const extensions = Object.keys(require.extensions);
9+
/* istanbul ignore if */
10+
if (!extensions.length) {
11+
// adapt for jest
12+
extensions.push('.js', '.node', '.json');
13+
if (process.env.EGG_TYPESCRIPT === 'true') {
14+
extensions.push('.ts');
15+
}
16+
}
817

918
module.exports = {
19+
extensions,
1020

1121
loadFile(filepath) {
1222
try {
1323
// if not js module, just return content buffer
1424
const extname = path.extname(filepath);
15-
if (extname && !require.extensions[extname]) {
25+
if (extname && !extensions.includes(extname)) {
1626
return fs.readFileSync(filepath);
1727
}
1828
// require js module

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"egg-utils": "^2.4.1",
5151
"eslint": "^4.19.1",
5252
"eslint-config-egg": "^7.0.0",
53+
"jest": "^23.6.0",
5354
"js-yaml": "^3.11.0",
5455
"mm": "^2.4.0",
5556
"mz-modules": "^2.1.0",

test/egg-ts.test.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ const mm = require('mm');
44
const request = require('supertest');
55
const assert = require('assert');
66
const utils = require('./utils');
7+
const loaderUtil = require('../lib/utils');
78

89
describe('test/egg-ts.test.js', () => {
910
let app;
1011

1112
beforeEach(() => {
1213
require.extensions['.ts'] = require.extensions['.js'];
14+
loaderUtil.extensions.push('.ts');
1315
});
1416

1517
afterEach(() => {
1618
mm.restore();
19+
loaderUtil.extensions.splice(loaderUtil.extensions.indexOf('.ts'), 1);
1720
delete require.extensions['.ts'];
1821
});
1922

@@ -99,10 +102,6 @@ describe('test/egg-ts.test.js', () => {
99102
});
100103
});
101104

102-
it('should support load ts file', async () => {
103-
104-
});
105-
106105
it('should not load d.ts files while typescript was true', async () => {
107106
mm(process.env, 'EGG_TYPESCRIPT', 'true');
108107
app = utils.createApp('egg-ts-js');
@@ -133,12 +132,9 @@ describe('test/egg-ts.test.js', () => {
133132

134133
it('should not load ts files while EGG_TYPESCRIPT was true but no extensions', async () => {
135134
mm(process.env, 'EGG_TYPESCRIPT', 'true');
136-
delete require.extensions['.ts'];
135+
mm(loaderUtil, 'extensions', [ '.js', '.json' ]);
137136
app = utils.createApp('egg-ts-js');
138-
139-
app.loader.loadApplicationExtend();
140137
app.loader.loadService();
141-
assert(!app.appExtend);
142138
assert(app.serviceClasses.lord);
143139
assert(!app.serviceClasses.test);
144140
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const assert = require('assert');
5+
const EggApplication = require('../').Application;
6+
7+
test('should works', async () => {
8+
const app = new EggApplication({
9+
baseDir: path.resolve(__dirname, '../'),
10+
type: 'application',
11+
});
12+
app.loader.loadAll();
13+
expect(!!app.Proxy).toBe(true);
14+
expect(!!app.config.urllib.keepAlive).toBe(true);
15+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
module.exports = {
4+
get Proxy() {
5+
return this.BaseContextClass;
6+
},
7+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
module.exports = function() {
4+
return (ctx, next) => {
5+
if (ctx.path === '/status') {
6+
ctx.body = 'egg status';
7+
return;
8+
}
9+
10+
return next();
11+
};
12+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
module.exports = {
4+
urllib: {
5+
keepAlive: true,
6+
keepAliveTimeout: 30000,
7+
timeout: 30000,
8+
maxSockets: Infinity,
9+
maxFreeSockets: 256,
10+
},
11+
12+
egg: 'egg',
13+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = {
4+
egg: 'egg-unittest',
5+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
5+
module.exports = {
6+
session: {
7+
enable: true,
8+
path: path.join(__dirname, '../node_modules/session'),
9+
},
10+
11+
hsfclient: {
12+
enable: false,
13+
path: path.join(__dirname, '../plugins/hsfclient'),
14+
},
15+
};

0 commit comments

Comments
 (0)