Skip to content

Commit

Permalink
feat(karma-runner): resolve local karma and ng version (#2622)
Browse files Browse the repository at this point in the history
Require `karma` or `@angular/cli` from the current working directory, instead of from `@stryker-mutator/karma-runner/src/util.js` (where the old `requireModule` function lived).
  • Loading branch information
nicojs authored Nov 18, 2020
1 parent 060d2dd commit 5b92130
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 19 deletions.
8 changes: 5 additions & 3 deletions packages/karma-runner/src/starters/angular-starter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import decamelize = require('decamelize');
import { Logger, LoggerFactoryMethod } from '@stryker-mutator/api/logging';
import * as semver from 'semver';

import { requireResolve } from '@stryker-mutator/util';

import { NgConfigOptions, NgTestArguments } from '../../src-generated/karma-runner-options';
import { requireModule } from '../utils';

const MIN_ANGULAR_CLI_VERSION = '6.1.0';

Expand All @@ -14,7 +15,7 @@ export async function start(getLogger: LoggerFactoryMethod, ngConfig?: NgConfigO
verifyAngularCliVersion();

// Make sure require angular cli from inside this function, that way it won't break if angular isn't installed and this file is required.
let cli = requireModule('@angular/cli');
let cli: any = requireResolve('@angular/cli');
if ('default' in cli) {
cli = cli.default;
}
Expand Down Expand Up @@ -47,7 +48,8 @@ export async function start(getLogger: LoggerFactoryMethod, ngConfig?: NgConfigO
}

function verifyAngularCliVersion() {
const version = semver.coerce(requireModule('@angular/cli/package').version);
const pkg = requireResolve('@angular/cli/package') as { version: string };
const version = semver.coerce(pkg.version);
if (!version || semver.lt(version, MIN_ANGULAR_CLI_VERSION)) {
throw new Error(`Your @angular/cli version (${version}) is not supported. Please install ${MIN_ANGULAR_CLI_VERSION} or higher`);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/karma-runner/src/starters/karma-starter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { requireModule } from '../utils';
import { requireResolve } from '@stryker-mutator/util';

export async function start(): Promise<void> {
// Make sure require karma from inside this function, that way it won't break if karma isn't installed and this file is required.
const karma = requireModule('karma');
const karma: any = requireResolve('karma');
await new karma.Server({
configFile: require.resolve('./stryker-karma.conf'),
}).start();
Expand Down
8 changes: 5 additions & 3 deletions packages/karma-runner/src/starters/stryker-karma.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import * as path from 'path';

import { Logger, LoggerFactoryMethod } from '@stryker-mutator/api/logging';
import { Config, ConfigOptions, ClientOptions, InlinePluginType } from 'karma';
import { noopLogger } from '@stryker-mutator/util';
import { noopLogger, requireResolve } from '@stryker-mutator/util';

import StrykerReporter from '../karma-plugins/stryker-reporter';
import TestHooksMiddleware, { TEST_HOOKS_FILE_NAME } from '../karma-plugins/test-hooks-middleware';
import { requireModule } from '../utils';

function setDefaultOptions(config: Config) {
config.set({
Expand All @@ -20,7 +19,10 @@ function setUserKarmaConfigFile(config: Config, log: Logger) {
const configFileName = path.resolve(globalSettings.karmaConfigFile);
log.debug('Importing config from "%s"', configFileName);
try {
const userConfig = requireModule(configFileName);
const userConfig = requireResolve(configFileName);
if (typeof userConfig !== 'function') {
throw new TypeError(`Karma config file "${configFileName}" should export a function! Found: ${typeof userConfig}`);
}
userConfig(config);
config.configFile = configFileName; // override config to ensure karma is as user-like as possible
} catch (error) {
Expand Down
3 changes: 0 additions & 3 deletions packages/karma-runner/src/utils.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { LoggerFactoryMethod } from '@stryker-mutator/api/logging';
import { testInjector } from '@stryker-mutator/test-helpers';
import * as utils from '@stryker-mutator/util';
import { expect } from 'chai';
import * as sinon from 'sinon';

import * as sut from '../../../src/starters/angular-starter';
import * as utils from '../../../src/utils';

describe('angularStarter', () => {
let requireModuleStub: sinon.SinonStub;
Expand All @@ -13,7 +13,7 @@ describe('angularStarter', () => {

beforeEach(() => {
cliStub = sinon.stub();
requireModuleStub = sinon.stub(utils, 'requireModule');
requireModuleStub = sinon.stub(utils, 'requireResolve');
requireModuleStub.withArgs('@angular/cli').returns(cliStub);
getLogger = () => testInjector.logger;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { testInjector } from '@stryker-mutator/test-helpers';
import { expect } from 'chai';
import { Config, ConfigOptions, ClientOptions } from 'karma';
import * as sinon from 'sinon';
import * as utils from '@stryker-mutator/util';

import sut = require('../../../src/starters/stryker-karma.conf');
import StrykerReporter from '../../../src/karma-plugins/stryker-reporter';
import TestHooksMiddleware, { TEST_HOOKS_FILE_NAME } from '../../../src/karma-plugins/test-hooks-middleware';
import * as utils from '../../../src/utils';

describe('stryker-karma.conf.js', () => {
let getLogger: sinon.SinonStub;
Expand All @@ -19,7 +19,7 @@ describe('stryker-karma.conf.js', () => {
config = new KarmaConfigMock();
getLogger = sinon.stub();
getLogger.returns(testInjector.logger);
requireModuleStub = sinon.stub(utils, 'requireModule');
requireModuleStub = sinon.stub(utils, 'requireResolve');
sut.setGlobals({
getLogger,
});
Expand Down
7 changes: 4 additions & 3 deletions packages/util/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
{
"type": "node",
"request": "launch",
"name": "Unit tests",
"name": "Tests",
"program": "${workspaceFolder}/../../node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test/helpers/**/*.js",
"${workspaceFolder}/test/unit/**/*.js"
"${workspaceFolder}/test/unit/**/*.js",
"${workspaceFolder}/test/integration/**/*.js"
],
"internalConsoleOptions": "openOnSessionStart",
"outFiles": [
Expand All @@ -41,4 +42,4 @@
]
}
]
}
}
4 changes: 3 additions & 1 deletion packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"description": "Contains utilities for Stryker, the mutation testing framework for JavaScript and friends",
"main": "src/index.js",
"scripts": {
"test": "nyc npm run test:unit",
"test": "nyc npm run test:all",
"test:all": "npm run test:unit && npm run test:integration",
"test:unit": "mocha \"test/unit/**/*.js\"",
"test:integration": "mocha \"test/integration/**/*.js\"",
"stryker": "node ../core/bin/stryker run"
},
"repository": {
Expand Down
1 change: 1 addition & 0 deletions packages/util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './flat-map';
export * from './i';
export * from './task';
export * from './directory-require-cache';
export * from './require-resolve';
7 changes: 7 additions & 0 deletions packages/util/src/require-resolve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Require a module from the current working directory (or a different base dir)
* @see https://nodejs.org/api/modules.html#modules_require_resolve_paths_request
*/
export function requireResolve(id: string, from = process.cwd()): unknown {
return require(require.resolve(id, { paths: [from] }));
}
26 changes: 26 additions & 0 deletions packages/util/test/integration/require-resolve.it.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import path = require('path');

import sinon = require('sinon');
import { expect } from 'chai';

import { requireResolve } from '../../src';

const resolveTestResource: typeof path.resolve = path.resolve.bind(path, __dirname, '..', '..', 'testResources', 'require-resolve');

describe(requireResolve.name, () => {
it('should be able to require from parent', () => {
const bar = requireResolve('bar', resolveTestResource());
expect(bar).eq('bar from parent');
});

it('should be able to require from child', () => {
const bar = requireResolve('bar', resolveTestResource('foo'));
expect(bar).eq('bar from foo');
});

it('should be able to require from current working directory', () => {
sinon.stub(process, 'cwd').returns(resolveTestResource('baz'));
const bar = requireResolve('bar');
expect(bar).eq('bar from baz');
});
});
1 change: 1 addition & 0 deletions packages/util/testResources/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!node_modules

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5b92130

Please sign in to comment.