Skip to content

Commit ae5a284

Browse files
chore: add some mocks test
1 parent d2770e4 commit ae5a284

File tree

6 files changed

+50
-74
lines changed

6 files changed

+50
-74
lines changed

__tests__/main.test.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

__tests__/mock-default.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useDefault } from '../src/main.js';
2+
3+
jest.mock('../src/mock.js', () => ({
4+
default: {
5+
methodBar: () => 'foo',
6+
methodFoo: () => 'bar',
7+
},
8+
}));
9+
10+
describe('mock-default', () => {
11+
it('should use mock value', () => {
12+
expect(useDefault().methodBar()).toBe('foo');
13+
expect(useDefault().methodFoo()).toBe('bar');
14+
});
15+
});

__tests__/mock-exports.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useMethodFoo, useMethodBar } from '../src/main.js';
2+
3+
jest.mock('../src/mock.js', () => ({
4+
methodBar: () => 'foo',
5+
methodFoo: () => 'bar',
6+
}));
7+
8+
describe('mock-exports', () => {
9+
it('should use mock value', () => {
10+
expect(useMethodFoo()).toBe('bar');
11+
expect(useMethodBar()).toBe('foo');
12+
});
13+
});

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export default {
22
testEnvironment: 'node',
33
preset: 'ts-jest/presets/default-esm',
44
transform: {
5-
'^.+\\.m?[tj]s?$': ['ts-jest', { useESM: true }],
5+
'^.+\\.m?[tj]s?$': ['ts-jest'],
66
},
77
moduleNameMapper: {
88
'^(\\.{1,2}/.*)\\.(m)?js$': '$1',

src/main.ts

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,13 @@
1-
/**
2-
* Some predefined delay values (in milliseconds).
3-
*/
4-
export enum Delays {
5-
Short = 500,
6-
Medium = 2000,
7-
Long = 5000,
8-
}
1+
import mock, { methodBar, methodFoo } from './mock.js';
92

10-
/**
11-
* Returns a Promise<string> that resolves after a given time.
12-
*
13-
* @param {string} name - A name.
14-
* @param {number=} [delay=Delays.Medium] - A number of milliseconds to delay resolution of the Promise.
15-
* @returns {Promise<string>}
16-
*/
17-
function delayedHello(
18-
name: string,
19-
delay: number = Delays.Medium,
20-
): Promise<string> {
21-
return new Promise((resolve: (value?: string) => void) =>
22-
setTimeout(() => resolve(`Hello, ${name}`), delay),
23-
);
24-
}
3+
export const useDefault = (): any => {
4+
return mock;
5+
};
256

26-
// Please see the comment in the .eslintrc.json file about the suppressed rule!
27-
// Below is an example of how to use ESLint errors suppression. You can read more
28-
// at https://eslint.org/docs/latest/user-guide/configuring/rules#disabling-rules
7+
export const useMethodBar = (): unknown => {
8+
return methodBar();
9+
};
2910

30-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
31-
export async function greeter(name: any) { // eslint-disable-line @typescript-eslint/no-explicit-any
32-
// The name parameter should be of type string. Any is used only to trigger the rule.
33-
return await delayedHello(name, Delays.Long);
34-
}
11+
export const useMethodFoo = (): unknown => {
12+
return methodFoo();
13+
};

src/mock.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const methodFoo = (): string => {
2+
return 'foo';
3+
};
4+
5+
export const methodBar = (): string => {
6+
return 'bar';
7+
};
8+
9+
const methods = { methodFoo, methodBar };
10+
11+
export default methods;

0 commit comments

Comments
 (0)