Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
fix: do not run afterXXX env methods without running beforeXXX (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman authored Apr 22, 2021
1 parent ad66639 commit 66de782
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 12 deletions.
29 changes: 21 additions & 8 deletions src/workerRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export class WorkerRunner extends EventEmitter {
stop() {
this._isStopped = true;
this._setCurrentTest(null);
if (this._envRunner)
this._envRunner.stop();
}

async cleanup() {
Expand Down Expand Up @@ -498,19 +500,24 @@ async function wrapInPromise(value: any) {
}

class EnvRunner {
private forward: Env[];
private backward: Env[];
private envs: Env[];
private workerArgs: any[] = [];
private testArgs: any[] = [];
private _isStopped = false;

constructor(envs: Env[]) {
this.forward = [...envs];
this.backward = [...envs].reverse();
this.envs = [...envs];
}

stop() {
this._isStopped = true;
}

async runBeforeAll(workerInfo: WorkerInfo, workerOptions: any) {
let args = {};
for (const env of this.forward) {
for (const env of this.envs) {
if (this._isStopped)
break;
if (env.beforeAll) {
const r = await env.beforeAll(mergeObjects(workerOptions, args), workerInfo);
args = mergeObjects(args, r);
Expand All @@ -522,8 +529,10 @@ class EnvRunner {

async runAfterAll(workerInfo: WorkerInfo, workerOptions: any) {
let error: Error | undefined;
for (const env of this.backward) {
const count = this.workerArgs.length;
for (let index = count - 1; index >= 0; index--) {
const args = this.workerArgs.pop();
const env = this.envs[index];
if (env.afterAll) {
try {
await env.afterAll(mergeObjects(workerOptions, args), workerInfo);
Expand All @@ -538,7 +547,9 @@ class EnvRunner {

async runBeforeEach(testInfo: TestInfo, testOptions: any) {
let args = {};
for (const env of this.forward) {
for (const env of this.envs) {
if (this._isStopped)
break;
if (env.beforeEach) {
const r = await env.beforeEach(mergeObjects(testOptions, args), testInfo);
args = mergeObjects(args, r);
Expand All @@ -550,7 +561,9 @@ class EnvRunner {

async runAfterEach(testInfo: TestInfo, testOptions: any) {
let error: Error | undefined;
for (const env of this.backward) {
const count = this.testArgs.length;
for (let index = count - 1; index >= 0; index--) {
const env = this.envs[index];
const args = this.testArgs.pop();
if (env.afterEach) {
try {
Expand Down
74 changes: 74 additions & 0 deletions test/env-errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,77 @@ test('can only call runWith in config file', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(1);
expect(result.output).toContain('runWith() can only be called in a configuration file');
});

test('should not run afterAll when did not run beforeAll', async ({ runInlineTest }) => {
const result = await runInlineTest({
'folio.config.ts': `
class MyEnv1 {
async beforeAll() {
console.log('beforeAll-1');
await new Promise(() => {});
}
async afterAll() {
console.log('afterAll-1');
}
}
class MyEnv2 {
async beforeAll() {
console.log('beforeAll-2');
}
async afterAll() {
console.log('afterAll-2');
}
}
export const test = folio.test.extend(new MyEnv1()).extend(new MyEnv2());
test.runWith();
folio.setConfig({ timeout: 1000 });
`,
'a.test.ts': `
import { test } from './folio.config';
test('test', async ({}) => {
});
`,
});
expect(result.exitCode).toBe(1);
expect(result.output).toContain('beforeAll-1');
expect(result.output).not.toContain('afterAll-1');
expect(result.output).not.toContain('beforeAll-2');
expect(result.output).not.toContain('afterAll-2');
});

test('should not run afterEach when did not run beforeEach', async ({ runInlineTest }) => {
const result = await runInlineTest({
'folio.config.ts': `
class MyEnv1 {
async beforeEach() {
console.log('beforeEach-1');
await new Promise(() => {});
}
async afterEach() {
console.log('afterEach-1');
}
}
class MyEnv2 {
async beforeEach() {
console.log('beforeEach-2');
}
async afterEach() {
console.log('afterEach-2');
}
}
export const test = folio.test.extend(new MyEnv1()).extend(new MyEnv2());
test.runWith();
folio.setConfig({ timeout: 1000 });
`,
'a.test.ts': `
import { test } from './folio.config';
test('test', async ({}) => {
});
`,
});
expect(result.exitCode).toBe(1);
expect(result.output).toContain('beforeEach-1');
expect(result.output).not.toContain('afterEach-1');
expect(result.output).not.toContain('beforeEach-2');
expect(result.output).not.toContain('afterEach-2');
});
20 changes: 20 additions & 0 deletions test/hooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,23 @@ test('beforeEach should be able to skip a test', async ({ runInlineTest }) => {
expect(passed).toBe(1);
expect(skipped).toBe(1);
});

test('beforeAll from a helper file should throw', async ({ runInlineTest }) => {
const result = await runInlineTest({
'my-test.ts': `
export const test = folio.test;
test.beforeAll(() => {});
`,
'folio.config.ts': `
import { test } from './my-test';
test.runWith();
`,
'a.test.ts': `
import { test } from './my-test';
test('should work', async () => {
});
`,
});
expect(result.exitCode).toBe(1);
expect(result.output).toContain('Hook can only be defined in a test file');
});
8 changes: 4 additions & 4 deletions test/stdio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import { test, expect } from './config';
test('should get top level stdio', async ({runInlineTest}) => {
const result = await runInlineTest({
'a.spec.js': `
console.log('%% top level stdout');
console.error('%% top level stderr');
console.log('\\n%% top level stdout');
console.error('\\n%% top level stderr');
test('is a test', () => {
console.log('%% stdout in a test');
console.error('%% stderr in a test');
console.log('\\n%% stdout in a test');
console.error('\\n%% stderr in a test');
});
`
});
Expand Down

0 comments on commit 66de782

Please sign in to comment.