Skip to content

Commit

Permalink
tests: add helpers to vitest (#1869)
Browse files Browse the repository at this point in the history
## Describe your changes

Fixes NAN-593

- Add reusable helpers to vitest
  • Loading branch information
bodinsamuel committed Mar 18, 2024
1 parent 7797512 commit 449ec1a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .env.example
Expand Up @@ -57,6 +57,8 @@ LOG_LEVEL=info
#
###############################################################################

TZ=UTC

# Configure where integrations will be loaded from
NANGO_INTEGRATIONS_FULL_PATH=

Expand Down
Expand Up @@ -25,10 +25,9 @@ describe('Records service', () => {
expect(success).toBe(true);
expect(error).toBe(null);
expect(response?.records.length).toBe(n);
const timestampRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{5,6}\+\d{2}:\d{2}$/;
expect(response?.records[0]?.['_nango_metadata']).toMatchObject({
first_seen_at: expect.stringMatching(timestampRegex),
last_modified_at: expect.stringMatching(timestampRegex),
first_seen_at: expect.toBeIsoDateTimezone(),
last_modified_at: expect.toBeIsoDateTimezone(),
last_action: 'ADDED',
deleted_at: null,
cursor: expect.stringMatching(/^[A-Za-z0-9+/]+={0,2}$/) // base64 encoded string
Expand Down
14 changes: 14 additions & 0 deletions packages/shared/lib/vitest.d.ts
@@ -0,0 +1,14 @@
export * from 'vitest';

/* eslint-disable @typescript-eslint/no-empty-interface */
interface CustomMatchers<TR = unknown> {
toBeIsoDate: () => TR;
toBeIsoDateTimezone: () => TR;
toBeUUID: () => TR;
}

declare module 'vitest' {
export interface Assertion<T = any> extends CustomMatchers<T> {}
export interface AsymmetricMatchersContaining extends CustomMatchers {}
export interface ExpectStatic<T = any> extends CustomMatchers<T> {}
}
6 changes: 5 additions & 1 deletion packages/shared/tsconfig.json
Expand Up @@ -4,6 +4,10 @@
"rootDir": ".",
"outDir": "dist"
},
"references": [{ "path": "../node-client" }],
"references": [
{
"path": "../node-client"
}
],
"include": ["lib/**/*"]
}
43 changes: 43 additions & 0 deletions tests/setupFiles.ts
@@ -0,0 +1,43 @@
import { expect } from 'vitest';

const dateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{1,6}Z$/;
const dateRegexWithTZ = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{1,6}\+\d{2}:\d{2}$/;
const uuidRegex = /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;

expect.extend({
toBeIsoDate: (received: any) => {
if (received instanceof Date) {
return { pass: true, message: () => '' };
} else if (typeof received === 'string' && dateRegex.test(received)) {
return { pass: true, message: () => '' };
}

return {
message: () => `expected ${received} to be an ISO Date`,
pass: false
};
},

toBeIsoDateTimezone: (received: any) => {
if (received instanceof Date) {
return { pass: true, message: () => '' };
} else if (typeof received === 'string' && dateRegexWithTZ.test(received)) {
return { pass: true, message: () => '' };
}

return {
message: () => `expected ${received} to be an ISO Date`,
pass: false
};
},

toBeUUID: (received: any) => {
if (!uuidRegex.test(received)) {
return {
message: () => `expected ${received} to be a UUID v4`,
pass: false
};
}
return { pass: true, message: () => '' };
}
});
4 changes: 3 additions & 1 deletion vite.integration.config.ts
@@ -1,13 +1,15 @@
/// <reference types="vitest" />

// Configure Vitest (https://vitest.dev/config/)

import { defineConfig } from 'vite';

process.env.TZ = 'UTC';

export default defineConfig({
test: {
include: ['**/*.integration.{test,spec}.?(c|m)[jt]s?(x)'],
globalSetup: './tests/setup.ts',
setupFiles: './tests/setupFiles.ts',
threads: false,
testTimeout: 20000
}
Expand Down

0 comments on commit 449ec1a

Please sign in to comment.