Skip to content

Commit

Permalink
fix(MockInstance): resetting scopes on each level
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Jan 22, 2023
1 parent 57957d9 commit ee2720d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/mock-instance-reset/backup.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
let globalVar = 0;

const backupTest = (newValue: typeof globalVar): void => {
let backup: typeof globalVar;

beforeEach(() => {
backup = globalVar;
globalVar = newValue;
});

afterEach(() => {
globalVar = backup;
});
};

describe('mock-instance-reset:backup', () => {
it('equals 0 before all', () => {
expect(globalVar).toEqual(0);
});

describe('globalVar', () => {
backupTest(1);
backupTest(2);

it('equals 2 before each', () => {
expect(globalVar).toEqual(2);
});

describe('each', () => {
backupTest(3);
backupTest(4);

it('equals 4 after each', () => {
expect(globalVar).toEqual(4);
});
});

it('resets to 2 after each', () => {
expect(globalVar).toEqual(2);
});
});

it('resets to 0 after all', () => {
expect(globalVar).toEqual(0);
});
});
68 changes: 68 additions & 0 deletions tests/mock-instance-reset/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const triggers: string[] = [];

describe('mock-instance-reset', () => {
describe('test', () => {
beforeAll(() => triggers.push('beforeAll:1'));
afterAll(() => triggers.push('afterAll:1'));

beforeAll(() => triggers.push('beforeAll:2'));
afterAll(() => triggers.push('afterAll:2'));

beforeEach(() => triggers.push('beforeEach:1'));
beforeEach(() => triggers.push('beforeEach:2'));

afterEach(() => triggers.push('afterEach:1'));
afterEach(() => triggers.push('afterEach:2'));

it('triggers test #1', () => {
expect(1).toEqual(1);
});

it('triggers test #2', () => {
expect(2).toEqual(2);
});

describe('nested', () => {
beforeAll(() => triggers.push('beforeAll:3'));
afterAll(() => triggers.push('afterAll:3'));

beforeEach(() => triggers.push('beforeEach:3'));
afterEach(() => triggers.push('afterEach:3'));

it('triggers test #3', () => {
expect(3).toEqual(3);
});
});
});

it('has expected order', () => {
// first before is called the first
// first after is called the last
expect(triggers).toEqual([
'beforeAll:1',
'beforeAll:2',

'beforeEach:1',
'beforeEach:2',
'afterEach:2',
'afterEach:1',

'beforeEach:1',
'beforeEach:2',
'afterEach:2',
'afterEach:1',

'beforeAll:3',
'beforeEach:1',
'beforeEach:2',
'beforeEach:3',
'afterEach:3',
'afterEach:2',
'afterEach:1',
'afterAll:3',

'afterAll:2',
'afterAll:1',
]);
});
});

0 comments on commit ee2720d

Please sign in to comment.