Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove espower typescript #160

Merged
merged 2 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions lib/cmd/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,6 @@ class TestCommand extends Command {
yield this.helper.forkNode(mochaFile, testArgs, opt);
}

get context() {
const context = super.context;
const { argv, execArgvObj } = context;

// remove ts-node, ts-node and espower-typescript can't coexist
// because espower-typescript@9 has already register ts-node
if (argv.typescript) {
execArgvObj.require.splice(execArgvObj.require.indexOf(require.resolve('ts-node/register')), 1);
}

return context;
}

/**
* format test args then change it to array style
* @param {Object} context - { cwd, argv, ...}
Expand Down Expand Up @@ -123,8 +110,9 @@ class TestCommand extends Command {

// for power-assert
if (testArgv.typescript) {
// remove ts-node in context getter on top.
requireArr.push(require.resolve('espower-typescript/guess'));
const tscompilerPath = require.resolve(testArgv.tscompiler);
requireArr.splice(requireArr.indexOf(tscompilerPath), 1);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tscompiler 必须跟在 espower-typescript 前面,才能保证堆栈正确

requireArr.push(tscompilerPath, require.resolve('../espower-typescript'));
}

testArgv.require = requireArr;
Expand Down
41 changes: 41 additions & 0 deletions lib/espower-typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const path = require('path');
const espowerSource = require('espower-source');
const minimatch = require('minimatch');
const sourceMapSupport = require('source-map-support');
const sourceCache = {};
const cwd = process.cwd();

espowerTypeScript({
pattern: path.resolve(cwd, 'test/**/*.ts'),
extensions: [ 'ts', 'tsx' ],
});

function espowerTypeScript(options) {
// install source-map-support again to correct the source-map
sourceMapSupport.install({
environment: 'node',
retrieveFile: p => sourceCache[p],
});

options.extensions.forEach(ext => {
espowerTsRegister(`.${ext}`, options);
});
}

function espowerTsRegister(ext, options) {
const originalExtension = require.extensions[ext];
require.extensions[ext] = (module, filepath) => {
if (!minimatch(filepath, options.pattern)) {
return originalExtension(module, filepath);
}
const originalCompile = module._compile;
module._compile = function(code, filepath) {
const newSource = espowerSource(code, filepath, options);
sourceCache[filepath] = newSource;
return originalCompile.call(this, newSource, filepath);
};
return originalExtension(module, filepath);
};
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@
"detect-port": "^1.3.0",
"egg-ts-helper": "^1.25.2",
"egg-utils": "^2.4.1",
"espower-typescript": "9.0.1",
"espower-source": "^2.3.0",
"globby": "^9.2.0",
"inspector-proxy": "^1.2.1",
"intelli-espower-loader": "^1.0.1",
"jest-changed-files": "^24.7.0",
"minimatch": "^3.0.4",
"mocha": "^6.0.2",
"mz-modules": "^2.1.0",
"nyc": "^13.3.0",
"power-assert": "^1.6.1",
"semver": "^6.0.0",
"source-map-support": "^0.5.19",
"test-exclude": "^5.1.0",
"ts-node": "^7",
"ypkgfiles": "^1.6.0"
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/example-ts-error-stack/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

// import * as assert from 'assert';
import assert = require('assert');

describe('test/index.test.ts', () => {
// placeholder comments
Expand Down
24 changes: 24 additions & 0 deletions test/ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ describe('test/ts.test.js', () => {
.expect('stdout', /error/)
.expect('stdout', /test\/index\.test\.ts:8:11\)/)
.expect('stdout', /test\/index\.test\.ts:14:5\)/)
.expect('stdout', /assert\(obj\.key === '222'\)/)
.expect('stdout', /| {3}| {3}|/)
.expect('stdout', /| {3}| {3}false/)
.expect('stdout', /| {3}"111"/)
.expect('stdout', /Object\{key:"111"}/)
.end();
});

it('should correct error stack line number in testing app with other tscompiler', () => {
return coffee.fork(eggBin, [ 'test', '--tscompiler=esbuild-register' ], { cwd })
.debug()
.expect('stdout', /error/)
.expect('stdout', /test\/index\.test\.ts:8:11\)/)
.expect('stdout', /test\/index\.test\.ts:14:5\)/)
.expect('stdout', /assert\(obj\.key === "222"\)/)
.expect('stdout', /| {3}| {3}|/)
.expect('stdout', /| {3}| {3}false/)
.expect('stdout', /| {3}"111"/)
.expect('stdout', /Object\{key:"111"}/)
.end();
});

Expand All @@ -119,6 +138,11 @@ describe('test/ts.test.js', () => {
.expect('stdout', /error/)
.expect('stdout', /test\/index\.test\.ts:8:11\)/)
.expect('stdout', /test\/index\.test\.ts:14:5\)/)
.expect('stdout', /assert\(obj\.key === '222'\)/)
.expect('stdout', /| {3}| {3}|/)
.expect('stdout', /| {3}| {3}false/)
.expect('stdout', /| {3}"111"/)
.expect('stdout', /Object\{key:"111"}/)
.end();
});
});
Expand Down