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(tests): rework vue integration tests #253

Merged
merged 2 commits into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
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