Skip to content

Commit

Permalink
test: import some jest tests/examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Jul 10, 2024
1 parent aaedc9f commit cf4636e
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 0 deletions.
11 changes: 11 additions & 0 deletions __test__/jest/jest-repo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Jest repo tests

A small subtree of the upstream Jest repo, snapshotted from:

<https://github.com/jestjs/jest/tree/21cce70205025decc778f4aac2ec76051f589fd7>

When updating, use the same commit or update the link and incorporate the changes

## package.json

Do not change, it's needed to make this subtree operate in CJS mode, which jest uses
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {expect, test} from '@jest/globals';
import '../toBeWithinRange';

test('is within range', () => expect(100).toBeWithinRange(90, 110));

test('is NOT within range', () => expect(101).not.toBeWithinRange(0, 100));

test('asymmetric ranges', () => {
expect({apples: 6, bananas: 3}).toEqual({
apples: expect.toBeWithinRange(1, 10),
bananas: expect.not.toBeWithinRange(11, 20),
});
});
56 changes: 56 additions & 0 deletions __test__/jest/jest-repo/examples/expect-extend/toBeWithinRange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {expect} from '@jest/globals';
import type {MatcherFunction} from 'expect';

const toBeWithinRange: MatcherFunction<[floor: unknown, ceiling: unknown]> =
function (actual, floor, ceiling) {
if (
typeof actual !== 'number' ||
typeof floor !== 'number' ||
typeof ceiling !== 'number'
) {
throw new TypeError('These must be of type number!');
}

const pass = actual >= floor && actual <= ceiling;
if (pass) {
return {
message: () =>
`expected ${this.utils.printReceived(
actual,
)} not to be within range ${this.utils.printExpected(
`${floor} - ${ceiling}`,
)}`,
pass: true,
};
} else {
return {
message: () =>
`expected ${this.utils.printReceived(
actual,
)} to be within range ${this.utils.printExpected(
`${floor} - ${ceiling}`,
)}`,
pass: false,
};
}
};

expect.extend({
toBeWithinRange,
});

declare module 'expect' {
interface AsymmetricMatchers {
toBeWithinRange(floor: number, ceiling: number): void;
}
interface Matchers<R> {
toBeWithinRange(floor: number, ceiling: number): R;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.. All Rights Reserved.

import user from '../models/user';

test('if original user model', () => {
expect(user.getAuthenticated()).toEqual({age: 26, name: 'Real name'});
});
8 changes: 8 additions & 0 deletions __test__/jest/jest-repo/examples/manual-mocks/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.. All Rights Reserved.

export default {
getAuthenticated: () => ({
age: 26,
name: 'Real name',
}),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.. All Rights Reserved.

'use strict';

jest.useFakeTimers();

it('schedules a 10-second timer after 1 second', () => {
jest.spyOn(globalThis, 'setTimeout');
const infiniteTimerGame = require('../infiniteTimerGame');
const callback = jest.fn();

infiniteTimerGame(callback);

// At this point in time, there should have been a single call to
// setTimeout to schedule the end of the game in 1 second.
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenNthCalledWith(1, expect.any(Function), 1000);

// Fast forward and exhaust only currently pending timers
// (but not any new timers that get created during that process)
jest.runOnlyPendingTimers();

// At this point, our 1-second timer should have fired its callback
expect(callback).toHaveBeenCalled();

// And it should have created a new timer to start the game over in
// 10 seconds
expect(setTimeout).toHaveBeenCalledTimes(2);
expect(setTimeout).toHaveBeenNthCalledWith(2, expect.any(Function), 10_000);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.. All Rights Reserved.

'use strict';

jest.useFakeTimers();

describe('timerGame', () => {
beforeEach(() => {
jest.spyOn(globalThis, 'setTimeout');
});
it('waits 1 second before ending the game', () => {
const timerGame = require('../timerGame');
timerGame();

expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 1000);
});

it('calls the callback after 1 second via runAllTimers', () => {
const timerGame = require('../timerGame');
const callback = jest.fn();

timerGame(callback);

// At this point in time, the callback should not have been called yet
expect(callback).not.toHaveBeenCalled();

// Fast-forward until all timers have been executed
jest.runAllTimers();

// Now our callback should have been called!
expect(callback).toHaveBeenCalled();
expect(callback).toHaveBeenCalledTimes(1);
});

it('calls the callback after 1 second via advanceTimersByTime', () => {
const timerGame = require('../timerGame');
const callback = jest.fn();

timerGame(callback);

// At this point in time, the callback should not have been called yet
expect(callback).not.toHaveBeenCalled();

// Fast-forward until all timers have been executed
jest.advanceTimersByTime(1000);

// Now our callback should have been called!
expect(callback).toHaveBeenCalled();
expect(callback).toHaveBeenCalledTimes(1);
});
});
17 changes: 17 additions & 0 deletions __test__/jest/jest-repo/examples/timer/infiniteTimerGame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.. All Rights Reserved.

function infiniteTimerGame(callback) {
console.log('Ready....go!');

setTimeout(() => {
console.log('Times up! 10 seconds before the next game starts...');
callback && callback();

// Schedule the next game in 10 seconds
setTimeout(() => {
infiniteTimerGame(callback);
}, 10_000);
}, 1000);
}

module.exports = infiniteTimerGame;
11 changes: 11 additions & 0 deletions __test__/jest/jest-repo/examples/timer/timerGame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.. All Rights Reserved.

function timerGame(callback) {
console.log('Ready....go!');
setTimeout(() => {
console.log('Times up -- stop!');
callback && callback();
}, 1000);
}

module.exports = timerGame;
13 changes: 13 additions & 0 deletions __test__/jest/jest-repo/examples/typescript/__tests__/sum.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.. All Rights Reserved.

import {expect, it} from '@jest/globals';

it('adds 1 + 2 to equal 3 in Typescript', () => {
const sum = require('../sum.ts').default;
expect(sum(1, 2)).toBe(3);
});

it('adds 1 + 2 to equal 3 in JavaScript', () => {
const sum = require('../sum.js');
expect(sum(1, 2)).toBe(3);
});
7 changes: 7 additions & 0 deletions __test__/jest/jest-repo/examples/typescript/sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.. All Rights Reserved.

function sum(a, b) {
return a + b;
}

module.exports = sum;
5 changes: 5 additions & 0 deletions __test__/jest/jest-repo/examples/typescript/sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

const sum = (a: number, b: number): number => a + b;

export default sum;
1 change: 1 addition & 0 deletions __test__/jest/jest-repo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "commonjs" }

0 comments on commit cf4636e

Please sign in to comment.