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

fix: export issue when testing with jest and ts-node #134

Merged
merged 4 commits into from
Feb 20, 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
16 changes: 16 additions & 0 deletions apps/nextjs/app/api/edge-no-context/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { GET } from './route';

describe('app API edge-no-context route', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => void 0);

beforeEach(() => jest.clearAllMocks());

it('tracks event', async () => {
const response = await GET(new Request(new URL('/', 'http://localhost')));
expect(response.status).toBe(200);
expect(log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledWith(
'[Vercel Web Analytics] Track "Edge Event" with data {"data":"edge","router":"app","manual":true}'
);
});
});
16 changes: 16 additions & 0 deletions apps/nextjs/app/api/edge/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { GET } from './route';

describe('app API edge route', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => void 0);

beforeEach(() => jest.clearAllMocks());

it('tracks event', async () => {
const response = await GET();
expect(response.status).toBe(200);
expect(log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledWith(
'[Vercel Web Analytics] Track "Edge Event" with data {"data":"edge","router":"app"}'
);
});
});
16 changes: 16 additions & 0 deletions apps/nextjs/app/api/serverless/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { GET } from './route';

describe('app API serverless route', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => void 0);

beforeEach(() => jest.clearAllMocks());

it('tracks event', async () => {
const response = await GET();
expect(response.status).toBe(200);
expect(log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledWith(
'[Vercel Web Analytics] Track "Serverless Event" with data {"data":"serverless","router":"app"}'
);
});
});
1 change: 1 addition & 0 deletions apps/nextjs/jest-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@testing-library/jest-dom';
17 changes: 17 additions & 0 deletions apps/nextjs/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Config } from 'jest';
import nextJest from 'next/jest.js';

const createJestConfig = nextJest({
dir: './',
});

const config: Config = {
coverageProvider: 'v8',
testRegex: '\\/.+\\.test\\.tsx?$',
setupFilesAfterEnv: ['<rootDir>/jest-setup.ts'],
transform: {
'^.+\\.(t|j)sx?$': '@swc/jest',
},
};

export default createJestConfig(config);
9 changes: 0 additions & 9 deletions apps/nextjs/next.config.js

This file was deleted.

10 changes: 9 additions & 1 deletion apps/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"build": "next build",
"dev": "next dev",
"start": "next start",
"test": "jest",
"test:e2e:development": "NEXT_PUBLIC_ANALYTICS_MODE=development pnpm --filter nextjs... build && playwright test development",
"test:e2e:production": "NEXT_PUBLIC_ANALYTICS_MODE=production pnpm --filter nextjs... build && playwright test production"
},
Expand All @@ -15,6 +16,13 @@
"react-dom": "18.2.0"
},
"devDependencies": {
"@playwright/test": "1.35.1"
"@playwright/test": "1.35.1",
"@swc/jest": "^0.2.26",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@types/jest": "^29.5.2",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"ts-node": "^10.9.2"
}
}
19 changes: 19 additions & 0 deletions apps/nextjs/pages/api/test.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { NextRequest } from 'next/server';
import GET from './test';

describe('pages API edge route', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => void 0);

beforeEach(() => jest.clearAllMocks());

it('tracks event', async () => {
const request = new NextRequest(new URL('/', 'http://localhost'));
// @ts-expect-error -- we should pass a NextFetchEvent instead an empty object, but it's not used.
const response = await GET(request, {});
expect(response.status).toBe(200);
expect(log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledWith(
'[Vercel Web Analytics] Track "Pages Api Route" with data {"runtime":"edge","router":"pages"}'
);
});
});
8 changes: 7 additions & 1 deletion apps/nextjs/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
],
"strictNullChecks": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
"./jest-setup.ts"
],
"exclude": ["node_modules"]
}
4 changes: 2 additions & 2 deletions packages/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vercel/analytics",
"version": "1.2.1",
"version": "1.2.2",
"description": "Gain real-time traffic insights with Vercel Web Analytics",
"keywords": [
"analytics",
Expand Down Expand Up @@ -29,7 +29,7 @@
"require": "./dist/next/index.js"
},
"./server": {
"node": "./dist/server/index.mjs",
"node": "./dist/server/index.js",
"edge-light": "./dist/server/index.mjs",
"import": "./dist/server/index.mjs",
"require": "./dist/server/index.js",
Expand Down
Loading
Loading