Skip to content

Commit 5ad2568

Browse files
phryneaspiotr-oles
authored andcommitted
fix(tests): rework vue integration tests
- do not create a IncrementalChecker instance on the test thread - use worker-rpc so the tests can actually access the checker instance run by the service - fix include path in "tsconfig-imports.json" so the "should resolve *.vue in the same way as TypeScript" test actually compiles files - add two counter checks to make sure the compiler actually throws errors it finds at all
1 parent 271278d commit 5ad2568

8 files changed

+449
-306
lines changed

test/integration/helpers.js

+29-24
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,27 @@ var fs = require('fs');
22
var path = require('path');
33
var webpack = require('webpack');
44
var ForkTsCheckerWebpackPlugin = require('../../lib/index');
5-
var IncrementalChecker = require('../../lib/IncrementalChecker')
6-
.IncrementalChecker;
7-
var NormalizedMessageFactories = require('../../lib/NormalizedMessageFactories');
5+
var RpcProvider = require('worker-rpc').RpcProvider;
86

97
var webpackMajorVersion = require('./webpackVersion')();
108
var VueLoaderPlugin =
119
webpackMajorVersion >= 4 ? require('vue-loader/lib/plugin') : undefined;
1210

13-
exports.createVueCompiler = function(options) {
11+
const rpcMethods = {
12+
checker_nextIteration: 'checker_nextIteration',
13+
checker_getKnownFileNames: 'checker_getKnownFileNames',
14+
checker_getSourceFile: 'checker_getSourceFile',
15+
checker_getSyntacticDiagnostics: 'checker_getSyntacticDiagnostics'
16+
};
17+
18+
exports.createVueCompiler = async function(options) {
1419
var plugin = new ForkTsCheckerWebpackPlugin({ ...options, silent: true });
20+
plugin.nodeArgs = [
21+
`--require`,
22+
`${path.resolve(__dirname, './mocks/IncrementalCheckerWithRpc.js')}`,
23+
`--require`,
24+
`${path.resolve(__dirname, './mocks/ApiIncrementalCheckerWithRpc.js')}`
25+
];
1526

1627
var compiler = webpack({
1728
...(webpackMajorVersion >= 4 ? { mode: 'development' } : {}),
@@ -57,27 +68,10 @@ exports.createVueCompiler = function(options) {
5768
'syntacticError.ts': path.resolve(compiler.context, 'src/syntacticError.ts')
5869
};
5970

60-
var checker = new IncrementalChecker(
61-
require('typescript'),
62-
NormalizedMessageFactories.makeCreateNormalizedMessageFromDiagnostic(
63-
require('typescript')
64-
),
65-
NormalizedMessageFactories.makeCreateNormalizedMessageFromRuleFailure,
66-
plugin.tsconfigPath,
67-
{},
68-
path.resolve(__dirname, './vue'),
69-
plugin.tslintPath || false,
70-
plugin.tslintAutoFix || false,
71-
[compiler.context],
72-
ForkTsCheckerWebpackPlugin.ONE_CPU,
73-
1,
74-
plugin.checkSyntacticErrors,
75-
plugin.vue
76-
);
77-
78-
checker.nextIteration();
71+
plugin.spawnService();
72+
await plugin.serviceRpc.rpc(rpcMethods.checker_nextIteration);
7973

80-
return { plugin, compiler, files, checker };
74+
return { plugin, compiler, files };
8175
};
8276

8377
exports.createCompiler = function(
@@ -194,3 +188,14 @@ exports.expectedErrorCodes = {
194188
expectedSyntacticErrorCode: 'TS1005',
195189
expectedSemanticErrorCode: 'TS2322'
196190
};
191+
192+
exports.rpcMethods = rpcMethods;
193+
194+
let rpc;
195+
exports.getRpcProvider = () => {
196+
if (!rpc) {
197+
rpc = new RpcProvider(message => process.send(message));
198+
process.on('message', message => rpc.dispatch(message));
199+
}
200+
return rpc;
201+
};

test/integration/incrementalApi.spec.js

+24-23
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,32 @@ describe('[INTEGRATION] specific tests for useTypescriptIncrementalApi: true', f
8888
});
8989

9090
it('should get syntactic diagnostics from Vue program', function(callback) {
91-
var { compiler } = createVueCompiler({ checkSyntacticErrors: true });
92-
93-
compiler.run(function(error, stats) {
94-
const syntacticErrorFoundInStats = stats.compilation.errors.some(error =>
95-
error.rawMessage.includes(
96-
helpers.expectedErrorCodes.expectedSyntacticErrorCode
97-
)
98-
);
99-
expect(syntacticErrorFoundInStats).to.be.true;
100-
callback();
101-
});
91+
createVueCompiler({ checkSyntacticErrors: true }).then(({ compiler }) =>
92+
compiler.run(function(error, stats) {
93+
const syntacticErrorFoundInStats = stats.compilation.errors.some(
94+
error =>
95+
error.rawMessage.includes(
96+
helpers.expectedErrorCodes.expectedSyntacticErrorCode
97+
)
98+
);
99+
expect(syntacticErrorFoundInStats).to.be.true;
100+
callback();
101+
})
102+
);
102103
});
103104

104105
it('should not find syntactic errors in Vue program when checkSyntacticErrors is false', function(callback) {
105-
var { compiler } = createVueCompiler({ checkSyntacticErrors: false });
106-
107-
compiler.run(function(error, stats) {
108-
const syntacticErrorNotFoundInStats = stats.compilation.errors.every(
109-
error =>
110-
!error.rawMessage.includes(
111-
helpers.expectedErrorCodes.expectedSyntacticErrorCode
112-
)
113-
);
114-
expect(syntacticErrorNotFoundInStats).to.be.true;
115-
callback();
116-
});
106+
createVueCompiler({ checkSyntacticErrors: false }).then(({ compiler }) =>
107+
compiler.run(function(error, stats) {
108+
const syntacticErrorNotFoundInStats = stats.compilation.errors.every(
109+
error =>
110+
!error.rawMessage.includes(
111+
helpers.expectedErrorCodes.expectedSyntacticErrorCode
112+
)
113+
);
114+
expect(syntacticErrorNotFoundInStats).to.be.true;
115+
callback();
116+
})
117+
);
117118
});
118119
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const mock = require('mock-require');
2+
3+
const origImport = require('../../../lib/ApiIncrementalChecker');
4+
const { rpcMethods, getRpcProvider } = require('../helpers');
5+
6+
mock('../../../lib/ApiIncrementalChecker', {
7+
ApiIncrementalChecker: class extends origImport.ApiIncrementalChecker {
8+
constructor(...args) {
9+
super(...args);
10+
11+
const rpc = getRpcProvider();
12+
13+
const awaitInit = async () => {
14+
if (!this.tsIncrementalCompiler.lastProcessing) {
15+
await this.tsIncrementalCompiler.processChanges();
16+
} else {
17+
await this.tsIncrementalCompiler.lastProcessing;
18+
}
19+
};
20+
21+
rpc.registerRpcHandler(rpcMethods.checker_nextIteration, () => {
22+
return this.nextIteration();
23+
});
24+
25+
rpc.registerRpcHandler(rpcMethods.checker_getKnownFileNames, async () => {
26+
await awaitInit();
27+
return Array.from(this.tsIncrementalCompiler.getAllKnownFiles());
28+
});
29+
30+
rpc.registerRpcHandler(
31+
rpcMethods.checker_getSourceFile,
32+
async fileName => {
33+
await awaitInit();
34+
const result = this.tsIncrementalCompiler
35+
.getProgram()
36+
.getSourceFile(fileName);
37+
return !result ? undefined : { text: result.text };
38+
}
39+
);
40+
41+
rpc.registerRpcHandler(
42+
rpcMethods.checker_getSyntacticDiagnostics,
43+
async () => {
44+
await awaitInit();
45+
const result = this.tsIncrementalCompiler
46+
.getProgram()
47+
.getSyntacticDiagnostics();
48+
return result.map(({ start, length, file }) => ({
49+
start,
50+
length,
51+
file: { text: file.text }
52+
}));
53+
}
54+
);
55+
}
56+
}
57+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const mock = require('mock-require');
2+
3+
const origImport = require('../../../lib/IncrementalChecker');
4+
5+
const { rpcMethods, getRpcProvider } = require('../helpers');
6+
7+
mock('../../../lib/IncrementalChecker', {
8+
IncrementalChecker: class extends origImport.IncrementalChecker {
9+
constructor(...args) {
10+
super(...args);
11+
12+
const rpc = getRpcProvider();
13+
14+
rpc.registerRpcHandler(rpcMethods.checker_nextIteration, () => {
15+
return this.nextIteration();
16+
});
17+
18+
rpc.registerRpcHandler(rpcMethods.checker_getKnownFileNames, () => {
19+
return this.programConfig.fileNames;
20+
});
21+
22+
rpc.registerRpcHandler(rpcMethods.checker_getSourceFile, fileName => {
23+
const result = this.program.getSourceFile(fileName);
24+
return !result ? undefined : { text: result.text };
25+
});
26+
27+
rpc.registerRpcHandler(rpcMethods.checker_getSyntacticDiagnostics, () => {
28+
const result = this.program.getSyntacticDiagnostics();
29+
return result.map(({ start, length, file }) => ({
30+
start,
31+
length,
32+
file: { text: file.text }
33+
}));
34+
});
35+
}
36+
}
37+
});

0 commit comments

Comments
 (0)