From 642e22248eceb815393131bfef6dedcdc751333b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20=C3=98sterkilde?= Date: Mon, 27 May 2024 13:51:09 +0200 Subject: [PATCH] chore(ci): add github workflow (#7) * chore(ci): add github workflow * chore(ci): make use of yarn & node 18 * chore(validathor): use fake timers in tests * chore(validathor): stop docs from accidentally being published * chore(ci): only build lib --- .github/workflows/ci.yml | 32 +++++++++++++++++++ apps/docs/package.json | 1 + apps/docs/pages/index.mdx | 4 +-- package.json | 1 + packages/validathor/README.md | 2 +- .../src/schemas/__tests__/date.test.ts | 24 ++++++++------ .../src/schemas/__tests__/string.test.ts | 2 +- packages/validathor/test/global-setup.ts | 3 ++ packages/validathor/vitest.config.ts | 1 + 9 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 packages/validathor/test/global-setup.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..6c68a63 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,32 @@ +name: CI + +on: [push] + +jobs: + unit_test-lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Setup Nodejs and npm + uses: actions/setup-node@v2 + with: + node-version: "18" + + - name: Setup yarn + run: npm install -g yarn + + - name: Setup Nodejs with yarn caching + uses: actions/setup-node@v2 + with: + node-version: "18" + cache: yarn + + - name: Install dependencies + run: yarn install --frozen-lockfile + + - run: | + yarn test + yarn lint + yarn type-check + yarn build:lib \ No newline at end of file diff --git a/apps/docs/package.json b/apps/docs/package.json index c9c0b90..ea09299 100644 --- a/apps/docs/package.json +++ b/apps/docs/package.json @@ -1,4 +1,5 @@ { + "private": true, "name": "@nordic-ui/docs", "version": "0.0.1", "scripts": { diff --git a/apps/docs/pages/index.mdx b/apps/docs/pages/index.mdx index 3cdc0f7..83fd369 100644 --- a/apps/docs/pages/index.mdx +++ b/apps/docs/pages/index.mdx @@ -38,8 +38,8 @@ Here's a basic example usecase }), acceptedTerms: boolean(), createdAt: date([ - minDate(new Date('2021/01/01')), - maxDate(new Date()), + min(new Date('2021/01/01')), + max(new Date()), ]), }); ``` diff --git a/package.json b/package.json index c74466c..26b2766 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "packageManager": "yarn@1.22.19", "scripts": { "build": "turbo run build", + "build:lib": "turbo run build --filter=validathor", "dev": "turbo run dev --no-cache --continue", "test": "turbo run test", "lint": "turbo run lint", diff --git a/packages/validathor/README.md b/packages/validathor/README.md index 83ef08e..dbac270 100644 --- a/packages/validathor/README.md +++ b/packages/validathor/README.md @@ -48,7 +48,7 @@ import { // Schemas object, string, number, boolean, date, // Modifiers - min, max, minLength, maxLength, email, + min, max, email, } from '@nordic-ui/validathor'; // Define your schema shape diff --git a/packages/validathor/src/schemas/__tests__/date.test.ts b/packages/validathor/src/schemas/__tests__/date.test.ts index 383606c..9424437 100644 --- a/packages/validathor/src/schemas/__tests__/date.test.ts +++ b/packages/validathor/src/schemas/__tests__/date.test.ts @@ -5,6 +5,14 @@ import { ValidationError, TypeError } from '@/utils/errors/errors' import { date } from '../date' describe('date()', () => { + beforeEach(() => { + vi.useFakeTimers() + }) + + afterEach(() => { + vi.useRealTimers() + }) + it('should be named correctly', () => { expect(date().name).toEqual('date') }) @@ -27,7 +35,7 @@ describe('date()', () => { expect(() => parse(schema, 123)).toThrowError(new ValidationError('Not a date')) }) - it('should work with minDate() and maxDate() modifiers', () => { + it('should work with min() and max() modifiers', () => { const schema1 = date([min(new Date('2021/01/01'))]) const schema2 = date([max(new Date('2021/12/31'))]) const schema3 = date([min(new Date('2021/01/01')), max(new Date('2021/12/31'))]) @@ -39,13 +47,13 @@ describe('date()', () => { expect(parse(schema3, new Date('2021/06/15'))).toEqual(new Date('2021/06/15')) expect(() => parse(schema1, new Date('2020/12/31'))).toThrowError( - new ValidationError('Value must be at least 2020-12-31T23:00:00.000Z'), + new ValidationError('Value must be at least 2021-01-01T00:00:00.000Z'), ) expect(() => parse(schema2, new Date('2022/01/01'))).toThrowError( - new ValidationError('Value must be at most 2021-12-30T23:00:00.000Z'), + new ValidationError('Value must be at most 2021-12-31T00:00:00.000Z'), ) expect(() => parse(schema3, new Date('2022/01/01'))).toThrowError( - new ValidationError('Value must be at most 2021-12-30T23:00:00.000Z'), + new ValidationError('Value must be at most 2021-12-31T00:00:00.000Z'), ) expect(() => parse(schema4, new Date('2020/12/31'))).toThrowError( new ValidationError('Date is too early'), @@ -57,13 +65,11 @@ describe('date()', () => { it('should work with custom() modifier', () => { const schema1 = date([ - custom((value) => [[value.getUTCDate() === 31, new TypeError('End of the month (maybe)')]]), + custom((value) => [[value.getDate() !== 31, 'End of the month (maybe)']]), ]) - expect(() => parse(schema1, new Date('2023/06/01'))).not.toThrowError( - new ValidationError('End of the month (maybe)'), - ) - expect(() => parse(schema1, new Date('2023/06/31'))).toThrowError( + expect(parse(schema1, new Date('2024/05/12'))).toEqual(new Date('2024/05/12')) + expect(() => parse(schema1, new Date('2024/05/31'))).toThrowError( new ValidationError('End of the month (maybe)'), ) }) diff --git a/packages/validathor/src/schemas/__tests__/string.test.ts b/packages/validathor/src/schemas/__tests__/string.test.ts index 105cf5e..b350eda 100644 --- a/packages/validathor/src/schemas/__tests__/string.test.ts +++ b/packages/validathor/src/schemas/__tests__/string.test.ts @@ -44,7 +44,7 @@ describe('string()', () => { ) }) - it('should work with minLength() and maxLength() modifiers', () => { + it('should work with min() and max() modifiers', () => { const schema1 = string([min(2)]) const schema2 = string([max(6)]) const schema3 = string([min(2), max(6)]) diff --git a/packages/validathor/test/global-setup.ts b/packages/validathor/test/global-setup.ts new file mode 100644 index 0000000..49069f2 --- /dev/null +++ b/packages/validathor/test/global-setup.ts @@ -0,0 +1,3 @@ +export const setup = () => { + process.env.TZ = 'UTC' +} diff --git a/packages/validathor/vitest.config.ts b/packages/validathor/vitest.config.ts index 8232073..8ed477b 100644 --- a/packages/validathor/vitest.config.ts +++ b/packages/validathor/vitest.config.ts @@ -12,5 +12,6 @@ export default defineConfig({ coverage: { reporter: ['text', 'json', 'html'], }, + globalSetup: './test/global-setup.ts', }, })