Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use vitest for connectors #5562

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/connectors/.gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# generated files
/*/types
/*/tsconfig.*
/*/jest.config.*
/*/rollup.config.*
/*/vitest.config.*

# keep templates
!/templates/**
5 changes: 2 additions & 3 deletions packages/connectors/connector-alipay-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
"dev": "tsc -p tsconfig.build.json --watch --preserveWatchOutput --incremental",
"lint": "eslint --ext .ts src",
"lint:report": "pnpm lint --format json --output-file report.json",
"test:only": "NODE_OPTIONS=--experimental-vm-modules jest",
"test": "pnpm build:test && pnpm test:only",
"test:ci": "pnpm test:only --silent --coverage",
"test": "vitest src",
"test:ci": "pnpm run test --silent --coverage",
"prepublishOnly": "pnpm build"
},
"engines": {
Expand Down
40 changes: 16 additions & 24 deletions packages/connectors/connector-alipay-native/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import { alipayEndpoint } from './constant.js';
import createConnector, { getAccessToken } from './index.js';
import { mockedAlipayNativeConfigWithValidPrivateKey } from './mock.js';

const { jest } = import.meta;

const getConfig = jest.fn().mockResolvedValue(mockedAlipayNativeConfigWithValidPrivateKey);
const getConfig = vi.fn().mockResolvedValue(mockedAlipayNativeConfigWithValidPrivateKey);

describe('getAuthorizationUri', () => {
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('should get a valid uri by state', async () => {
Expand All @@ -26,7 +24,7 @@ describe('getAuthorizationUri', () => {
jti: 'dummy-jti',
headers: {},
},
jest.fn()
vi.fn()
);
expect(authorizationUri).toBe('alipay://?app_id=2021000000000000&state=dummy-state');
});
Expand All @@ -35,7 +33,7 @@ describe('getAuthorizationUri', () => {
describe('getAccessToken', () => {
afterEach(() => {
nock.cleanAll();
jest.clearAllMocks();
vi.clearAllMocks();
});

const alipayEndpointUrl = new URL(alipayEndpoint);
Expand Down Expand Up @@ -72,7 +70,7 @@ describe('getAccessToken', () => {
sign: '<signature>',
});
const connector = await createConnector({ getConfig });
await expect(connector.getUserInfo({}, jest.fn())).rejects.toMatchError(
await expect(connector.getUserInfo({}, vi.fn())).rejects.toStrictEqual(
new ConnectorError(ConnectorErrorCodes.General, '{}')
);
});
Expand All @@ -93,7 +91,7 @@ describe('getAccessToken', () => {
});
await expect(
getAccessToken('code', mockedAlipayNativeConfigWithValidPrivateKey)
).rejects.toMatchError(new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid));
).rejects.toStrictEqual(new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid));
});

it('should fail with wrong code', async () => {
Expand All @@ -110,7 +108,7 @@ describe('getAccessToken', () => {
});
await expect(
getAccessToken('wrong_code', mockedAlipayNativeConfigWithValidPrivateKey)
).rejects.toMatchError(
).rejects.toStrictEqual(
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, 'Invalid code')
);
});
Expand All @@ -136,7 +134,7 @@ describe('getUserInfo', () => {

afterEach(() => {
nock.cleanAll();
jest.clearAllMocks();
vi.clearAllMocks();
});

const alipayEndpointUrl = new URL(alipayEndpoint);
Expand All @@ -158,7 +156,7 @@ describe('getUserInfo', () => {
const connector = await createConnector({ getConfig });
const { id, name, avatar, rawData } = await connector.getUserInfo(
{ auth_code: 'code' },
jest.fn()
vi.fn()
);
expect(id).toEqual('2088000000000000');
expect(name).toEqual('PlayboyEric');
Expand Down Expand Up @@ -189,9 +187,7 @@ describe('getUserInfo', () => {
sign: '<signature>',
});
const connector = await createConnector({ getConfig });
await expect(
connector.getUserInfo({ auth_code: 'wrong_code' }, jest.fn())
).rejects.toMatchError(
await expect(connector.getUserInfo({ auth_code: 'wrong_code' }, vi.fn())).rejects.toStrictEqual(
new ConnectorError(ConnectorErrorCodes.SocialAccessTokenInvalid, 'Invalid auth token')
);
});
Expand All @@ -210,9 +206,7 @@ describe('getUserInfo', () => {
sign: '<signature>',
});
const connector = await createConnector({ getConfig });
await expect(
connector.getUserInfo({ auth_code: 'wrong_code' }, jest.fn())
).rejects.toMatchError(
await expect(connector.getUserInfo({ auth_code: 'wrong_code' }, vi.fn())).rejects.toStrictEqual(
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, 'Invalid auth code')
);
});
Expand All @@ -231,9 +225,7 @@ describe('getUserInfo', () => {
sign: '<signature>',
});
const connector = await createConnector({ getConfig });
await expect(
connector.getUserInfo({ auth_code: 'wrong_code' }, jest.fn())
).rejects.toMatchError(
await expect(connector.getUserInfo({ auth_code: 'wrong_code' }, vi.fn())).rejects.toStrictEqual(
new ConnectorError(ConnectorErrorCodes.General, {
errorDescription: 'Invalid parameter',
code: '40002',
Expand All @@ -258,14 +250,14 @@ describe('getUserInfo', () => {
sign: '<signature>',
});
const connector = await createConnector({ getConfig });
await expect(
connector.getUserInfo({ auth_code: 'wrong_code' }, jest.fn())
).rejects.toMatchError(new ConnectorError(ConnectorErrorCodes.InvalidResponse));
await expect(connector.getUserInfo({ auth_code: 'wrong_code' }, vi.fn())).rejects.toStrictEqual(
new ConnectorError(ConnectorErrorCodes.InvalidResponse)
);
});

it('should throw with other request errors', async () => {
nock(alipayEndpointUrl.origin).post(alipayEndpointUrl.pathname).query(true).reply(500);
const connector = await createConnector({ getConfig });
await expect(connector.getUserInfo({ auth_code: 'wrong_code' }, jest.fn())).rejects.toThrow();
await expect(connector.getUserInfo({ auth_code: 'wrong_code' }, vi.fn())).rejects.toThrow();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import {
} from './mock.js';
import { signingParameters } from './utils.js';

const { jest } = import.meta;

const listenJSONParse = jest.spyOn(JSON, 'parse');
const listenJSONStringify = jest.spyOn(JSON, 'stringify');
const listenJSONParse = vi.spyOn(JSON, 'parse');
const listenJSONStringify = vi.spyOn(JSON, 'stringify');

describe('signingParameters', () => {
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

const testingParameters = {
Expand Down
5 changes: 2 additions & 3 deletions packages/connectors/connector-alipay-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
"dev": "tsc -p tsconfig.build.json --watch --preserveWatchOutput --incremental",
"lint": "eslint --ext .ts src",
"lint:report": "pnpm lint --format json --output-file report.json",
"test:only": "NODE_OPTIONS=--experimental-vm-modules jest",
"test": "pnpm build:test && pnpm test:only",
"test:ci": "pnpm test:only --silent --coverage",
"test": "vitest src",
"test:ci": "pnpm run test --silent --coverage",
"prepublishOnly": "pnpm build"
},
"engines": {
Expand Down
36 changes: 14 additions & 22 deletions packages/connectors/connector-alipay-web/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import { alipayEndpoint, authorizationEndpoint } from './constant.js';
import createConnector, { getAccessToken } from './index.js';
import { mockedAlipayConfigWithValidPrivateKey } from './mock.js';

const { jest } = import.meta;

const getConfig = jest.fn().mockResolvedValue(mockedAlipayConfigWithValidPrivateKey);
const getConfig = vi.fn().mockResolvedValue(mockedAlipayConfigWithValidPrivateKey);

describe('getAuthorizationUri', () => {
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('should get a valid uri by redirectUri and state', async () => {
Expand All @@ -26,7 +24,7 @@ describe('getAuthorizationUri', () => {
jti: 'some_jti',
headers: {},
},
jest.fn()
vi.fn()
);
expect(authorizationUri).toEqual(
`${authorizationEndpoint}?app_id=2021000000000000&redirect_uri=http%3A%2F%2Flocalhost%3A3001%2Fcallback&scope=auth_user&state=some_state`
Expand All @@ -37,7 +35,7 @@ describe('getAuthorizationUri', () => {
describe('getAccessToken', () => {
afterEach(() => {
nock.cleanAll();
jest.clearAllMocks();
vi.clearAllMocks();
});

const alipayEndpointUrl = new URL(alipayEndpoint);
Expand Down Expand Up @@ -78,7 +76,7 @@ describe('getAccessToken', () => {

await expect(
getAccessToken('code', mockedAlipayConfigWithValidPrivateKey)
).rejects.toMatchError(new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid));
).rejects.toStrictEqual(new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid));
});

it('should fail with wrong code', async () => {
Expand All @@ -96,7 +94,7 @@ describe('getAccessToken', () => {

await expect(
getAccessToken('wrong_code', mockedAlipayConfigWithValidPrivateKey)
).rejects.toMatchError(
).rejects.toStrictEqual(
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, 'Invalid code')
);
});
Expand All @@ -105,7 +103,7 @@ describe('getAccessToken', () => {
describe('getUserInfo', () => {
afterEach(() => {
nock.cleanAll();
jest.clearAllMocks();
vi.clearAllMocks();
});

beforeEach(() => {
Expand Down Expand Up @@ -144,7 +142,7 @@ describe('getUserInfo', () => {
const connector = await createConnector({ getConfig });
const { id, name, avatar, rawData } = await connector.getUserInfo(
{ auth_code: 'code' },
jest.fn()
vi.fn()
);
expect(id).toEqual('2088000000000000');
expect(name).toEqual('PlayboyEric');
Expand All @@ -163,7 +161,7 @@ describe('getUserInfo', () => {

it('throw General error if auth_code not provided in input', async () => {
const connector = await createConnector({ getConfig });
await expect(connector.getUserInfo({}, jest.fn())).rejects.toMatchError(
await expect(connector.getUserInfo({}, vi.fn())).rejects.toStrictEqual(
new ConnectorError(ConnectorErrorCodes.InvalidResponse, '{}')
);
});
Expand All @@ -182,9 +180,7 @@ describe('getUserInfo', () => {
sign: '<signature>',
});
const connector = await createConnector({ getConfig });
await expect(
connector.getUserInfo({ auth_code: 'wrong_code' }, jest.fn())
).rejects.toMatchError(
await expect(connector.getUserInfo({ auth_code: 'wrong_code' }, vi.fn())).rejects.toStrictEqual(
new ConnectorError(ConnectorErrorCodes.SocialAccessTokenInvalid, 'Invalid auth token')
);
});
Expand All @@ -203,9 +199,7 @@ describe('getUserInfo', () => {
sign: '<signature>',
});
const connector = await createConnector({ getConfig });
await expect(
connector.getUserInfo({ auth_code: 'wrong_code' }, jest.fn())
).rejects.toMatchError(
await expect(connector.getUserInfo({ auth_code: 'wrong_code' }, vi.fn())).rejects.toStrictEqual(
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, 'Invalid auth code')
);
});
Expand All @@ -224,9 +218,7 @@ describe('getUserInfo', () => {
sign: '<signature>',
});
const connector = await createConnector({ getConfig });
await expect(
connector.getUserInfo({ auth_code: 'wrong_code' }, jest.fn())
).rejects.toMatchError(
await expect(connector.getUserInfo({ auth_code: 'wrong_code' }, vi.fn())).rejects.toStrictEqual(
new ConnectorError(ConnectorErrorCodes.General, {
errorDescription: 'Invalid parameter',
code: '40002',
Expand All @@ -251,14 +243,14 @@ describe('getUserInfo', () => {
sign: '<signature>',
});
const connector = await createConnector({ getConfig });
await expect(connector.getUserInfo({ auth_code: 'code' }, jest.fn())).rejects.toMatchError(
await expect(connector.getUserInfo({ auth_code: 'code' }, vi.fn())).rejects.toStrictEqual(
new ConnectorError(ConnectorErrorCodes.InvalidResponse)
);
});

it('should throw with other request errors', async () => {
nock(alipayEndpointUrl.origin).post(alipayEndpointUrl.pathname).query(true).reply(500);
const connector = await createConnector({ getConfig });
await expect(connector.getUserInfo({ auth_code: 'code' }, jest.fn())).rejects.toThrow();
await expect(connector.getUserInfo({ auth_code: 'code' }, vi.fn())).rejects.toThrow();
});
});
8 changes: 3 additions & 5 deletions packages/connectors/connector-alipay-web/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import { methodForAccessToken } from './constant.js';
import { mockedAlipayConfigWithValidPrivateKey, mockedAlipayPublicParameters } from './mock.js';
import { signingParameters } from './utils.js';

const { jest } = import.meta;

const listenJSONParse = jest.spyOn(JSON, 'parse');
const listenJSONStringify = jest.spyOn(JSON, 'stringify');
const listenJSONParse = vi.spyOn(JSON, 'parse');
const listenJSONStringify = vi.spyOn(JSON, 'stringify');

describe('signingParameters', () => {
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

const testingParameters = {
Expand Down
5 changes: 2 additions & 3 deletions packages/connectors/connector-aliyun-dm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
"dev": "tsc -p tsconfig.build.json --watch --preserveWatchOutput --incremental",
"lint": "eslint --ext .ts src",
"lint:report": "pnpm lint --format json --output-file report.json",
"test:only": "NODE_OPTIONS=--experimental-vm-modules jest",
"test": "pnpm build:test && pnpm test:only",
"test:ci": "pnpm test:only --silent --coverage",
"test": "vitest src",
"test:ci": "pnpm run test --silent --coverage",
"prepublishOnly": "pnpm build"
},
"engines": {
Expand Down
10 changes: 4 additions & 6 deletions packages/connectors/connector-aliyun-dm/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@ import { TemplateType } from '@logto/connector-kit';

import { mockedConfigWithAllRequiredTemplates } from './mock.js';

const { jest } = import.meta;
const getConfig = vi.fn().mockResolvedValue(mockedConfigWithAllRequiredTemplates);

const getConfig = jest.fn().mockResolvedValue(mockedConfigWithAllRequiredTemplates);

const singleSendMail = jest.fn(() => ({
const singleSendMail = vi.fn(() => ({
body: JSON.stringify({ EnvId: 'env-id', RequestId: 'request-id' }),
statusCode: 200,
}));

jest.unstable_mockModule('./single-send-mail.js', () => ({
vi.mock('./single-send-mail.js', () => ({
singleSendMail,
}));

const { default: createConnector } = await import('./index.js');

describe('sendMessage()', () => {
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('should call singleSendMail() with correct template and content', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const { jest } = import.meta;
const request = vi.fn();

const request = jest.fn();

jest.unstable_mockModule('./utils.js', () => ({
vi.mock('./utils.js', () => ({
request,
}));

Expand Down
6 changes: 2 additions & 4 deletions packages/connectors/connector-aliyun-dm/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { mockedParameters } from './mock.js';

const { jest } = import.meta;
const post = vi.fn();

const post = jest.fn();

jest.unstable_mockModule('got', () => ({
vi.mock('got', () => ({
got: { post },
}));

Expand Down
Loading
Loading