Skip to content

Commit

Permalink
fix(tests): rework vue integration tests
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
phryneas authored and piotr-oles committed Apr 22, 2019
1 parent 271278d commit 5ad2568
Show file tree
Hide file tree
Showing 8 changed files with 449 additions and 306 deletions.
53 changes: 29 additions & 24 deletions test/integration/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@ var fs = require('fs');
var path = require('path');
var webpack = require('webpack');
var ForkTsCheckerWebpackPlugin = require('../../lib/index');
var IncrementalChecker = require('../../lib/IncrementalChecker')
.IncrementalChecker;
var NormalizedMessageFactories = require('../../lib/NormalizedMessageFactories');
var RpcProvider = require('worker-rpc').RpcProvider;

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

exports.createVueCompiler = function(options) {
const rpcMethods = {
checker_nextIteration: 'checker_nextIteration',
checker_getKnownFileNames: 'checker_getKnownFileNames',
checker_getSourceFile: 'checker_getSourceFile',
checker_getSyntacticDiagnostics: 'checker_getSyntacticDiagnostics'
};

exports.createVueCompiler = async function(options) {
var plugin = new ForkTsCheckerWebpackPlugin({ ...options, silent: true });
plugin.nodeArgs = [
`--require`,
`${path.resolve(__dirname, './mocks/IncrementalCheckerWithRpc.js')}`,
`--require`,
`${path.resolve(__dirname, './mocks/ApiIncrementalCheckerWithRpc.js')}`
];

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

var checker = new IncrementalChecker(
require('typescript'),
NormalizedMessageFactories.makeCreateNormalizedMessageFromDiagnostic(
require('typescript')
),
NormalizedMessageFactories.makeCreateNormalizedMessageFromRuleFailure,
plugin.tsconfigPath,
{},
path.resolve(__dirname, './vue'),
plugin.tslintPath || false,
plugin.tslintAutoFix || false,
[compiler.context],
ForkTsCheckerWebpackPlugin.ONE_CPU,
1,
plugin.checkSyntacticErrors,
plugin.vue
);

checker.nextIteration();
plugin.spawnService();
await plugin.serviceRpc.rpc(rpcMethods.checker_nextIteration);

return { plugin, compiler, files, checker };
return { plugin, compiler, files };
};

exports.createCompiler = function(
Expand Down Expand Up @@ -194,3 +188,14 @@ exports.expectedErrorCodes = {
expectedSyntacticErrorCode: 'TS1005',
expectedSemanticErrorCode: 'TS2322'
};

exports.rpcMethods = rpcMethods;

let rpc;
exports.getRpcProvider = () => {
if (!rpc) {
rpc = new RpcProvider(message => process.send(message));
process.on('message', message => rpc.dispatch(message));
}
return rpc;
};
47 changes: 24 additions & 23 deletions test/integration/incrementalApi.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,31 +88,32 @@ describe('[INTEGRATION] specific tests for useTypescriptIncrementalApi: true', f
});

it('should get syntactic diagnostics from Vue program', function(callback) {
var { compiler } = createVueCompiler({ checkSyntacticErrors: true });

compiler.run(function(error, stats) {
const syntacticErrorFoundInStats = stats.compilation.errors.some(error =>
error.rawMessage.includes(
helpers.expectedErrorCodes.expectedSyntacticErrorCode
)
);
expect(syntacticErrorFoundInStats).to.be.true;
callback();
});
createVueCompiler({ checkSyntacticErrors: true }).then(({ compiler }) =>
compiler.run(function(error, stats) {
const syntacticErrorFoundInStats = stats.compilation.errors.some(
error =>
error.rawMessage.includes(
helpers.expectedErrorCodes.expectedSyntacticErrorCode
)
);
expect(syntacticErrorFoundInStats).to.be.true;
callback();
})
);
});

it('should not find syntactic errors in Vue program when checkSyntacticErrors is false', function(callback) {
var { compiler } = createVueCompiler({ checkSyntacticErrors: false });

compiler.run(function(error, stats) {
const syntacticErrorNotFoundInStats = stats.compilation.errors.every(
error =>
!error.rawMessage.includes(
helpers.expectedErrorCodes.expectedSyntacticErrorCode
)
);
expect(syntacticErrorNotFoundInStats).to.be.true;
callback();
});
createVueCompiler({ checkSyntacticErrors: false }).then(({ compiler }) =>
compiler.run(function(error, stats) {
const syntacticErrorNotFoundInStats = stats.compilation.errors.every(
error =>
!error.rawMessage.includes(
helpers.expectedErrorCodes.expectedSyntacticErrorCode
)
);
expect(syntacticErrorNotFoundInStats).to.be.true;
callback();
})
);
});
});
57 changes: 57 additions & 0 deletions test/integration/mocks/ApiIncrementalCheckerWithRpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const mock = require('mock-require');

const origImport = require('../../../lib/ApiIncrementalChecker');
const { rpcMethods, getRpcProvider } = require('../helpers');

mock('../../../lib/ApiIncrementalChecker', {
ApiIncrementalChecker: class extends origImport.ApiIncrementalChecker {
constructor(...args) {
super(...args);

const rpc = getRpcProvider();

const awaitInit = async () => {
if (!this.tsIncrementalCompiler.lastProcessing) {
await this.tsIncrementalCompiler.processChanges();
} else {
await this.tsIncrementalCompiler.lastProcessing;
}
};

rpc.registerRpcHandler(rpcMethods.checker_nextIteration, () => {
return this.nextIteration();
});

rpc.registerRpcHandler(rpcMethods.checker_getKnownFileNames, async () => {
await awaitInit();
return Array.from(this.tsIncrementalCompiler.getAllKnownFiles());
});

rpc.registerRpcHandler(
rpcMethods.checker_getSourceFile,
async fileName => {
await awaitInit();
const result = this.tsIncrementalCompiler
.getProgram()
.getSourceFile(fileName);
return !result ? undefined : { text: result.text };
}
);

rpc.registerRpcHandler(
rpcMethods.checker_getSyntacticDiagnostics,
async () => {
await awaitInit();
const result = this.tsIncrementalCompiler
.getProgram()
.getSyntacticDiagnostics();
return result.map(({ start, length, file }) => ({
start,
length,
file: { text: file.text }
}));
}
);
}
}
});
37 changes: 37 additions & 0 deletions test/integration/mocks/IncrementalCheckerWithRpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const mock = require('mock-require');

const origImport = require('../../../lib/IncrementalChecker');

const { rpcMethods, getRpcProvider } = require('../helpers');

mock('../../../lib/IncrementalChecker', {
IncrementalChecker: class extends origImport.IncrementalChecker {
constructor(...args) {
super(...args);

const rpc = getRpcProvider();

rpc.registerRpcHandler(rpcMethods.checker_nextIteration, () => {
return this.nextIteration();
});

rpc.registerRpcHandler(rpcMethods.checker_getKnownFileNames, () => {
return this.programConfig.fileNames;
});

rpc.registerRpcHandler(rpcMethods.checker_getSourceFile, fileName => {
const result = this.program.getSourceFile(fileName);
return !result ? undefined : { text: result.text };
});

rpc.registerRpcHandler(rpcMethods.checker_getSyntacticDiagnostics, () => {
const result = this.program.getSyntacticDiagnostics();
return result.map(({ start, length, file }) => ({
start,
length,
file: { text: file.text }
}));
});
}
}
});
Loading

0 comments on commit 5ad2568

Please sign in to comment.