diff --git a/tests/mock-instance-reset/backup.spec.ts b/tests/mock-instance-reset/backup.spec.ts new file mode 100644 index 0000000000..b36589dd0c --- /dev/null +++ b/tests/mock-instance-reset/backup.spec.ts @@ -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); + }); +}); diff --git a/tests/mock-instance-reset/test.spec.ts b/tests/mock-instance-reset/test.spec.ts new file mode 100644 index 0000000000..feb23995cd --- /dev/null +++ b/tests/mock-instance-reset/test.spec.ts @@ -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', + ]); + }); +});