Skip to content

Commit

Permalink
Migrate to vitest (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Apr 30, 2023
1 parent 59b5f28 commit ed954e0
Show file tree
Hide file tree
Showing 16 changed files with 845 additions and 2,734 deletions.
6 changes: 0 additions & 6 deletions .codecov.yml

This file was deleted.

42 changes: 4 additions & 38 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,42 +45,8 @@ jobs:
- name: Install Dependencies
run: pnpm install --frozen-lockfile --prefer-offline

- run: pnpm build
- name: Build
run: pnpm build

test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: '18'

- name: Setup pnpm
uses: pnpm/action-setup@v2.2.4
with:
version: 8
run_install: false

- name: Get pnpm store directory
id: pnpm-store
run: echo "pnpm_cache_dir=$(pnpm store path)" >> $GITHUB_OUTPUT

- name: Use pnpm store
uses: actions/cache@v3
id: pnpm-cache
with:
path: ${{ steps.pnpm-store.outputs.pnpm_cache_dir }}
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-
- name: Install Dependencies
run: pnpm install --frozen-lockfile --prefer-offline
- run: pnpm test
- uses: codecov/codecov-action@v1
- name: Test
run: pnpm test
2 changes: 0 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ example
yarn.lock
pnpm-lock.yaml
.rpt2_cache
coverage
packages
typings.d.ts
Dockerfile
Expand All @@ -17,7 +16,6 @@ docker-compose.yml
.eslintrc.json
.prettierignore
/website
jest.preact.config.json
/docs
.rts2_cache_es
.rts2_cache_umd
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

[![npm version](https://badgen.net/npm/v/hoofd)](https://www.npmjs.com/package/hoofd)
[![Bundle size](https://badgen.net/bundlephobia/minzip/hoofd)](https://badgen.net/bundlephobia/minzip/hoofd)
[![codecov](https://codecov.io/gh/JoviDeCroock/hoofd/branch/main/graph/badge.svg)](https://codecov.io/gh/JoviDeCroock/hoofd)

This project aims at providing a set of hooks to populate `<meta>`, ... for each page. With crawlers now supporting
client-side alterations it's important to support a fallback model for our `<head>` tags. The dispatcher located in this
Expand Down Expand Up @@ -128,8 +127,8 @@ const stringify = (title, metas, links) => {
`${acc}<${tagName}${Object.keys(tag).reduce(
(properties, key) => `${properties} ${key}="${tag[key]}"`,
''
)}>`
}, '')
)}>`;
}, '');

return `
<title>${title}</title>
Expand Down
39 changes: 16 additions & 23 deletions __tests__/ssr.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
jest.mock('../src/utils', () => {
import { expect, describe, it, vi } from 'vitest';

vi.mock('../src/utils', () => {
return {
isServerSide: true,
};
});
import * as React from 'react';
import * as ReactDom from 'react-dom/server';
import {
useTitle,
toStatic,
Expand All @@ -13,22 +16,12 @@ import {
useHead,
useScript,
} from '../src';
import { render } from '@testing-library/react';

describe('ssr', () => {
let original: any;

beforeAll(() => {
original = React.useEffect;
(React as any).useEffect = jest.fn(() => {});
});

afterAll(() => {
(React as any).useEffect = original;
});
const render = ReactDom.renderToString;

describe('ssr', () => {
it('should render to string (basic-individual)', () => {
jest.useFakeTimers();
vi.useFakeTimers();
const MyComponent = () => {
useTitle('hi');
useLang('nl');
Expand All @@ -49,7 +42,7 @@ describe('ssr', () => {
};

render(<MyComponent />);
jest.runAllTimers();
vi.runAllTimers();
const { lang, title, metas, links, scripts } = toStatic();

expect(lang).toEqual('nl');
Expand All @@ -74,15 +67,15 @@ describe('ssr', () => {
});

it('should escape meta-content', () => {
jest.useFakeTimers();
vi.useFakeTimers();
const MyComponent = () => {
useMeta({ property: 'fb:admins', content: "''<>&" });
useMeta({ property: 'fb:something', content: '""' });
return <p>hi</p>;
};

render(<MyComponent />);
jest.runAllTimers();
vi.runAllTimers();
const { metas } = toStatic();

expect(metas).toEqual([
Expand All @@ -92,7 +85,7 @@ describe('ssr', () => {
});

it('should render to string (basic-useHead)', () => {
jest.useFakeTimers();
vi.useFakeTimers();
const MyComponent = () => {
useHead({
title: 'hi',
Expand All @@ -102,14 +95,14 @@ describe('ssr', () => {
};

render(<MyComponent />);
jest.runAllTimers();
vi.runAllTimers();
const { title, metas } = toStatic();
expect(title).toEqual('hi');
expect(metas).toEqual([{ content: 'hi', property: 'fb:admins' }]);
});

it('should render to string (nested-individual)', () => {
jest.useFakeTimers();
vi.useFakeTimers();
const MyComponent = (props: any) => {
useTitle('hi');
useMeta({ property: 'fb:admins', content: 'hi' });
Expand Down Expand Up @@ -142,7 +135,7 @@ describe('ssr', () => {
<MyNestedComponent />
</MyComponent>
);
jest.runAllTimers();
vi.runAllTimers();
const { title, metas, links, scripts } = toStatic();
expect(title).toEqual('bye');
expect(metas).toEqual([{ content: 'bye', property: 'fb:admins' }]);
Expand All @@ -166,7 +159,7 @@ describe('ssr', () => {
});

it('should render to string (nested-useHead)', () => {
jest.useFakeTimers();
vi.useFakeTimers();
const MyComponent = (props: any) => {
useHead({
title: 'hi',
Expand All @@ -188,7 +181,7 @@ describe('ssr', () => {
<MyNestedComponent />
</MyComponent>
);
jest.runAllTimers();
vi.runAllTimers();
const { title, metas } = toStatic();
expect(title).toEqual('bye');
expect(metas).toEqual([{ content: 'bye', property: 'fb:admins' }]);
Expand Down
29 changes: 10 additions & 19 deletions __tests__/ssr.useTitleTemplate.test.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,32 @@
jest.mock('../src/utils', () => {
import { expect, describe, it, vi } from 'vitest';

vi.mock('../src/utils', () => {
return {
isServerSide: true,
};
});

import * as React from 'react';
import * as ReactDom from 'react-dom/server';
import { useTitle, useTitleTemplate, toStatic } from '../src';
import { render } from '@testing-library/react';

describe('ssr with a template', () => {
let original: any;

beforeAll(() => {
original = React.useEffect;
(React as any).useEffect = jest.fn(() => {});
});

afterAll(() => {
(React as any).useEffect = original;
});

it('should render to string (basic)', () => {
jest.useFakeTimers();
vi.useFakeTimers();
const MyComponent = () => {
useTitleTemplate('%s | you');
useTitle('hi');
return <p>hi</p>;
};

render(<MyComponent />);
jest.runAllTimers();
ReactDom.renderToString(<MyComponent />);
vi.runAllTimers();
const { title } = toStatic();
expect(title).toEqual('hi | you');
});

it('should render to string (nested)', () => {
jest.useFakeTimers();
vi.useFakeTimers();
const MyComponent = (props: any) => {
useTitleTemplate('%s | you');
useTitle('hi');
Expand All @@ -48,12 +39,12 @@ describe('ssr with a template', () => {
return <p>hi</p>;
};

render(
ReactDom.renderToString(
<MyComponent>
<MyNestedComponent />
</MyComponent>
);
jest.runAllTimers();
vi.runAllTimers();
const { title } = toStatic();
expect(title).toEqual('bye | you');
});
Expand Down
22 changes: 13 additions & 9 deletions __tests__/useHead.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import '@testing-library/jest-dom';
/**
* @vitest-environment jsdom
*/
import * as React from 'react';
import { act, render, cleanup } from '@testing-library/react';
import { expect, describe, afterEach, it, vi } from 'vitest';

import { useHead } from '../src';
import dispatcher from '../src/dispatcher';

Expand All @@ -11,7 +15,7 @@ describe('useMeta', () => {
});

it('should use full head', async () => {
jest.useFakeTimers();
vi.useFakeTimers();
const MyComponent = ({ description }: any) => {
useHead({
title: 'Hello world',
Expand All @@ -34,7 +38,7 @@ describe('useMeta', () => {
));
});

jest.runAllTimers();
vi.runAllTimers();
expect(document.title).toEqual('Hello world');
expect(document.head.innerHTML).toContain('<meta charset="utf-8">');
expect(document.head.innerHTML).toContain(
Expand All @@ -51,7 +55,7 @@ describe('useMeta', () => {
await rerender(<MyComponent description="This is not a test" />);
});

jest.runAllTimers();
vi.runAllTimers();
expect(document.title).toEqual('Hello world');
expect(document.head.innerHTML).toContain('<meta charset="utf-8">');
expect(document.head.innerHTML).toContain(
Expand All @@ -72,7 +76,7 @@ describe('useMeta', () => {
});

it('should deeply utilize head', async () => {
jest.useFakeTimers();
vi.useFakeTimers();

const MyChild = ({ description, title }: any) => {
useHead({
Expand Down Expand Up @@ -114,7 +118,7 @@ describe('useMeta', () => {
));
});

jest.runAllTimers();
vi.runAllTimers();
expect(
document.getElementsByTagName('html')[0].getAttribute('lang')
).toEqual('en');
Expand All @@ -138,7 +142,7 @@ describe('useMeta', () => {
);
});

jest.runAllTimers();
vi.runAllTimers();
expect(document.title).toEqual('Hello world');
expect(document.head.innerHTML).toContain('<meta charset="utf-8">');
expect(document.head.innerHTML).toContain(
Expand All @@ -159,7 +163,7 @@ describe('useMeta', () => {
);
});

jest.runAllTimers();
vi.runAllTimers();
expect(document.title).toEqual('Ohaio world');
expect(document.head.innerHTML).toContain('<meta charset="utf-8">');
expect(document.head.innerHTML).toContain(
Expand All @@ -176,7 +180,7 @@ describe('useMeta', () => {
await rerender(<MyComponent twitter description="This is not a test" />);
});

jest.runAllTimers();
vi.runAllTimers();
expect(document.title).toEqual('Hello world');
expect(document.head.innerHTML).toContain('<meta charset="utf-8">');
expect(document.head.innerHTML).toContain(
Expand Down
6 changes: 5 additions & 1 deletion __tests__/useLanguage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import '@testing-library/jest-dom';
/**
* @vitest-environment jsdom
*/
import * as React from 'react';
import { act, render, cleanup } from '@testing-library/react';
import { expect, describe, afterEach, it } from 'vitest';

import { useLang } from '../src';

describe('useLang', () => {
Expand Down
10 changes: 7 additions & 3 deletions __tests__/useLink.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import '@testing-library/jest-dom';
/**
* @vitest-environment jsdom
*/
import * as React from 'react';
import { act, render, cleanup } from '@testing-library/react';
import { expect, describe, afterEach, it } from 'vitest';

import { useLink } from '../src';

describe('useLink', () => {
Expand Down Expand Up @@ -68,7 +72,7 @@ describe('useLink', () => {
rel: 'prefetch',
sizes: 'x',
};
Object.keys(options).forEach((key) => {
Object.keys(options).forEach(key => {
// @ts-ignore
(node as Element).setAttribute(key, options[key]);
});
Expand Down Expand Up @@ -118,7 +122,7 @@ describe('useLink', () => {
rel: 'prefetch',
sizes: 'x',
};
Object.keys(options).forEach((key) => {
Object.keys(options).forEach(key => {
// @ts-ignore
(node as Element).setAttribute(key, options[key]);
});
Expand Down
Loading

0 comments on commit ed954e0

Please sign in to comment.