Skip to content

Commit

Permalink
fix(jasmine-runner): update deprecated api calls (#2250)
Browse files Browse the repository at this point in the history
Removes some calls to deprecated Jasmine APIs.

| Old | New |
|--|--|
| `throwOnExpectationFailure` | `jasmine.Env.configure( { oneFailurePerSpec: true } )` |
| `stopOnSpecFailure` | `jasmine.Env.configure( { failFast: true } )`  | 
| `jasmine.addReporter` | `jasmine.env.addReporter` |
| `jasmine.clearReporters` | `jasmine.env.clearReporters` |
  • Loading branch information
nicojs committed Jun 10, 2020
1 parent d463f86 commit b6d6dfd
Show file tree
Hide file tree
Showing 12 changed files with 1,010 additions and 162 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[{*.ts,*.js,*jsx,*tsx,*.json,*.code-workspace}]
[{*.ts,*.js,*jsx,*tsx,*.json,,*.jsonc,*.code-workspace}]
insert_final_newline = true
indent_style = space
indent_size = 2
Expand Down
7 changes: 7 additions & 0 deletions packages/jasmine-runner/.mocharc.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"spec": [
"test/unit/**/*.js",
"test/integration/**/*.js"
],
"file": "test/helpers/setupTests.js"
}
30 changes: 6 additions & 24 deletions packages/jasmine-runner/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,20 @@
{
"type": "node",
"request": "launch",
"name": "Unit tests",
"name": "Unit/integration tests",
"program": "${workspaceFolder}/../../node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test/helpers/**/*.js",
"${workspaceFolder}/test/unit/**/*.js"
"--no-timeout",
"--colors"
],
"internalConsoleOptions": "openOnSessionStart",
"outFiles": [
"${workspaceRoot}/test/**/*.js",
"${workspaceRoot}/src/**/*.js"
]
},
{
"type": "node",
"request": "launch",
"name": "Integration tests",
"program": "${workspaceFolder}/../../node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test/helpers/**/*.js",
"${workspaceFolder}/test/integration/**/*.js"
],
"internalConsoleOptions": "openOnSessionStart",
"outFiles": [
"${workspaceRoot}/test/**/*.js",
"${workspaceRoot}/src/**/*.js"
"skipFiles": [
"<node_internals>/**"
]
}
]
}
}
2 changes: 1 addition & 1 deletion packages/jasmine-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "src/index.js",
"scripts": {
"test": "nyc --exclude-after-remap=false --check-coverage --reporter=html --report-dir=reports/coverage --lines 100 --functions 100 --branches 85 npm run mocha",
"mocha": "mocha \"test/helpers/**/*.js\" \"test/unit/**/*.js\" \"test/integration/**/*.js\"",
"mocha": "mocha",
"stryker": "node ../core/bin/stryker run"
},
"nyc": {
Expand Down
10 changes: 6 additions & 4 deletions packages/jasmine-runner/src/JasmineTestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class JasmineTestRunner implements TestRunner {
});
},
};
jasmine.addReporter(reporter);
jasmine.env.addReporter(reporter);
jasmine.execute();
}).catch((error) => ({
errorMessages: [`An error occurred while loading your jasmine specs${EOL}${errorToString(error)}`],
Expand All @@ -58,11 +58,13 @@ export default class JasmineTestRunner implements TestRunner {
const jasmine = new Jasmine({ projectBaseDir: process.cwd() });
// The `loadConfigFile` will fallback on the default
jasmine.loadConfigFile(this.jasmineConfigFile);
jasmine.stopSpecOnExpectationFailure(true);
jasmine.env.throwOnExpectationFailure(true);
jasmine.env.configure({
failFast: true,
oneFailurePerSpec: true,
});

jasmine.exit = () => {};
jasmine.clearReporters();
jasmine.env.clearReporters();
jasmine.randomizeTests(false);
return jasmine;
}
Expand Down
6 changes: 0 additions & 6 deletions packages/jasmine-runner/test/helpers/initChai.ts

This file was deleted.

7 changes: 0 additions & 7 deletions packages/jasmine-runner/test/helpers/initSinon.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/jasmine-runner/test/helpers/initSourceMaps.ts

This file was deleted.

50 changes: 50 additions & 0 deletions packages/jasmine-runner/test/helpers/mockFactories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import sinon = require('sinon');

export function createRunDetails(): jasmine.RunDetails {
return {
failedExpectations: [],
order: {
random: false,
seed: '12foo',
sort: (_) => _,
} as jasmine.Order,
};
}

export function createEnvStub(): sinon.SinonStubbedInstance<jasmine.Env> {
return {
currentSpec: sinon.stub(),
matchersClass: sinon.stub(),
version: sinon.stub(),
versionString: sinon.stub(),
nextSpecId: sinon.stub(),
addReporter: sinon.stub(),
execute: sinon.stub(),
describe: sinon.stub(),
beforeEach: sinon.stub(),
beforeAll: sinon.stub(),
currentRunner: sinon.stub(),
afterEach: sinon.stub(),
afterAll: sinon.stub(),
xdescribe: sinon.stub(),
it: sinon.stub(),
xit: sinon.stub(),
compareRegExps_: sinon.stub(),
compareObjects_: sinon.stub(),
equals_: sinon.stub(),
contains_: sinon.stub(),
addCustomEqualityTester: sinon.stub(),
addMatchers: sinon.stub(),
specFilter: sinon.stub(),
throwOnExpectationFailure: sinon.stub(),
stopOnSpecFailure: sinon.stub(),
seed: sinon.stub(),
provideFallbackReporter: sinon.stub(),
throwingExpectationFailures: sinon.stub(),
allowRespy: sinon.stub(),
randomTests: sinon.stub(),
randomizeTests: sinon.stub(),
clearReporters: sinon.stub(),
configure: sinon.stub(),
};
}
14 changes: 14 additions & 0 deletions packages/jasmine-runner/test/helpers/setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'source-map-support/register';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as sinonChai from 'sinon-chai';
import { testInjector } from '@stryker-mutator/test-helpers';
import * as sinon from 'sinon';

chai.use(sinonChai);
chai.use(chaiAsPromised);

afterEach(() => {
sinon.reset();
testInjector.reset();
});
62 changes: 35 additions & 27 deletions packages/jasmine-runner/test/unit/JasmineTestRunner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,35 @@ import Jasmine = require('jasmine');
import * as helpers from '../../src/helpers';
import JasmineTestRunner from '../../src/JasmineTestRunner';
import { expectTestResultsToEqual } from '../helpers/assertions';
import { createEnvStub, createRunDetails } from '../helpers/mockFactories';

type SinonStubbedInstance<TType> = {
[P in keyof TType]: TType[P] extends Function ? sinon.SinonStub : TType[P];
};

describe('JasmineTestRunner', () => {
let sandbox: sinon.SinonSandbox;
let jasmineStub: SinonStubbedInstance<Jasmine>;
let jasmineEnvStub: SinonStubbedInstance<jasmine.Env>;
let evalGlobalStub: sinon.SinonStub;
let sut: JasmineTestRunner;
let fileNames: string[];
let clock: sinon.SinonFakeTimers;

beforeEach(() => {
sandbox = sinon.createSandbox();
jasmineStub = sandbox.createStubInstance(Jasmine);
jasmineStub.env = {
throwOnExpectationFailure: sandbox.stub(),
};
evalGlobalStub = sandbox.stub(helpers, 'evalGlobal');
sandbox.stub(helpers, 'Jasmine').returns(jasmineStub);
jasmineStub = sinon.createStubInstance(Jasmine);
jasmineEnvStub = createEnvStub();
jasmineStub.env = (jasmineEnvStub as unknown) as jasmine.Env;
evalGlobalStub = sinon.stub(helpers, 'evalGlobal');
sinon.stub(helpers, 'Jasmine').returns(jasmineStub);
fileNames = ['foo.js', 'bar.js'];
clock = sandbox.useFakeTimers();
clock = sinon.useFakeTimers();
sut = new JasmineTestRunner(fileNames, factory.strykerOptions({ jasmineConfigFile: 'jasmineConfFile' }));
});

afterEach(() => {
delete require.cache['foo.js'];
delete require.cache['bar.js'];
sandbox.restore();
sinon.restore();
});

it('should configure jasmine on run', async () => {
Expand All @@ -46,10 +45,12 @@ describe('JasmineTestRunner', () => {
expect(helpers.Jasmine).calledWithNew;
expect(helpers.Jasmine).calledWith({ projectBaseDir: process.cwd() });
expect(jasmineStub.loadConfigFile).calledWith('jasmineConfFile');
expect(jasmineStub.stopSpecOnExpectationFailure).calledWith(true);
expect(jasmineStub.env.throwOnExpectationFailure).calledWith(true);
expect(jasmineStub.exit).ok;
expect(jasmineStub.clearReporters).called;
expect(jasmineStub.env.configure).calledWith({
failFast: true,
oneFailurePerSpec: true,
});
expect(jasmineStub.exit).ok.and.not.eq(process.exit);
expect(jasmineStub.env.clearReporters).called;
expect(jasmineStub.randomizeTests).calledWith(false);
});

Expand All @@ -64,14 +65,20 @@ describe('JasmineTestRunner', () => {
it('should report completed specs', async () => {
// Arrange
function addReporter(rep: jasmine.CustomReporter) {
rep.specDone({ id: 'spec0', fullName: 'foo spec', status: 'success' });
rep.specDone({ id: 'spec1', fullName: 'bar spec', status: 'failure', failedExpectations: [{ message: 'bar failed' }] });
rep.specDone({ id: 'spec2', fullName: 'disabled', status: 'disabled' });
rep.specDone({ id: 'spec3', fullName: 'pending', status: 'pending' });
rep.specDone({ id: 'spec4', fullName: 'excluded', status: 'excluded' });
rep.jasmineDone();
rep.specDone!({ id: 'spec0', fullName: 'foo spec', status: 'success', description: 'string' });
rep.specDone!({
id: 'spec1',
fullName: 'bar spec',
status: 'failure',
failedExpectations: [{ actual: 'foo', expected: 'bar', matcherName: 'fooMatcher', passed: false, message: 'bar failed', stack: 'stack' }],
description: 'string',
});
rep.specDone!({ id: 'spec2', fullName: 'disabled', status: 'disabled', description: 'string' });
rep.specDone!({ id: 'spec3', fullName: 'pending', status: 'pending', description: 'string' });
rep.specDone!({ id: 'spec4', fullName: 'excluded', status: 'excluded', description: 'string' });
rep.jasmineDone!(createRunDetails());
}
jasmineStub.addReporter.callsFake(addReporter);
jasmineEnvStub.addReporter.callsFake(addReporter);

// Act
const result = await sut.run({});
Expand All @@ -89,12 +96,13 @@ describe('JasmineTestRunner', () => {

it('should time spec duration', async () => {
function addReporter(rep: jasmine.CustomReporter) {
rep.specStarted();
const spec = { fullName: 'foobar spec', id: 'spec0', description: '' };
rep.specStarted!(spec);
clock.tick(10);
rep.specDone({ fullName: 'foobar spec', id: 'spec0' });
rep.jasmineDone();
rep.specDone!(spec);
rep.jasmineDone!(createRunDetails());
}
jasmineStub.addReporter.callsFake(addReporter);
jasmineEnvStub.addReporter.callsFake(addReporter);
const result = await sut.run({});
const expectedTestResult: TestResult = {
failureMessages: undefined,
Expand Down Expand Up @@ -124,9 +132,9 @@ describe('JasmineTestRunner', () => {

function actRunWithoutTests(testHooks?: string) {
function addReporter(rep: jasmine.CustomReporter) {
rep.jasmineDone();
rep.jasmineDone!(createRunDetails());
}
jasmineStub.addReporter.callsFake(addReporter);
jasmineEnvStub.addReporter.callsFake(addReporter);
return sut.run({ testHooks });
}
});
Loading

0 comments on commit b6d6dfd

Please sign in to comment.