Skip to content

Commit

Permalink
working type and e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
victorgarciaesgi committed Jan 15, 2024
1 parent 5f98d5d commit fcc52d3
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 85 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"release": "bumpp && npm publish && git push --follow-tags",
"test:prepare-fixtures": "nuxi prepare test/fixtures/sample-project",
"test:types-fixtures": "nuxi typecheck test/fixtures/sample-project",
"test:fixtures": "vitest run --dir test",
"test:fixtures": "vitest run --dir test ",
"test:types": "pnpm run typecheck",
"test": "pnpm run test:prepare-fixtures && pnpm run test:types && pnpm run test:fixtures",
"test:debug": "NUXT_ROUTER_CONFIG_NAME=$CONFIG pnpm run test:types-fixtures"
Expand Down
15 changes: 15 additions & 0 deletions test/e2e/e2eTests.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import path from 'path';
import { setupNuxtE2ETestForConfig } from './setupNuxtE2ETestForConfig';
import { globby } from 'globby';

describe.sequential('Testing E2E with different configs', async () => {
try {
const allConfigs = await globby('test/samples-config');
for (let config of allConfigs) {
const configFileName = path.parse(config).name;
await setupNuxtE2ETestForConfig(configFileName);
}
} catch (e: any) {
throw new Error(e);
}
});
11 changes: 0 additions & 11 deletions test/e2e/sample-project/allConfigs.spec.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import { timeout } from '$$/utils';
import { getBrowser, url, useTestContext } from '@nuxt/test-utils';
import { getConfigFromName } from '$$/utils';
import { getBrowser, setup, url, useTestContext, $fetch } from '@nuxt/test-utils';
import { fileURLToPath } from 'url';
import { expect } from 'vitest';
import { $ } from 'zx';

export async function setupNuxtTestWithConfig(configName: string) {
try {
// await $`NUXT_ROUTER_CONFIG_NAME=${configName} pnpm run test:prepare-fixtures`;
// await timeout(500);
await $`NUXT_ROUTER_CONFIG_NAME=${configName} pnpm run test:types-fixtures`;
await timeout(100);
} catch (e) {
return Promise.reject(`Typecheck failed for config: [${configName}]: ${e}`);
}
// it('should render base buttons', async () => {
// const link = `/[${configName}]`;
// const html = await $fetch(link);
export async function setupNuxtE2ETestForConfig(configName: string, basePath = '') {
const testConfig = await import(`../samples-config/${configName}.ts`);
await setup({
rootDir: fileURLToPath(new URL('../fixtures/sample-project', import.meta.url)),
nuxtConfig: getConfigFromName(configName, testConfig),
});

it(
`should render base buttons for config [${configName}]`,
async () => {
const link = `/tests/${basePath}[${configName}]`;
const html = await $fetch(link);

// expect(html).toContain('Navigate button');
// expect(html).toContain('Navigate link');
// expect(html).toContain('NavigateTo button');
expect(html).toContain('Navigate button');
expect(html).toContain('Navigate link');
expect(html).toContain('NavigateTo button');

// await expectNoClientErrors(link);
// });
await expectNoClientErrors(link);
},
{ sequential: true }
);
}

// Taken from nuxt/framework repo
Expand Down
22 changes: 2 additions & 20 deletions test/fixtures/sample-project/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
import type { NuxtConfig } from '@nuxt/schema';
import TestModuleRoute from './src/modules/testAddRoute';
import { globbySync } from 'globby';
import { getConfigFromName } from '../../utils';

const configName = process.env.NUXT_ROUTER_CONFIG_NAME;

let additionalConfig: NuxtConfig | undefined;

console.log({ configName });

if (configName) {
const config = require(`../../samples-config/${configName}.ts`);
const files = globbySync(`test/fixtures/sample-project/tests/[${configName}]`);
additionalConfig = {
...config,
nuxtTypedRouter: {
...config.nuxtTypedRouter,
ignoreRoutes: ['[...404].vue', '[tests]/**/*.vue'],
},
typescript: {
tsConfig: {
files: [
...files.map((m) => m.replace('test/fixtures/sample-project', '..')),
`../src/pages/[tests]/[${configName}].vue`,
],
exclude: ['../tests/**/*', '../src/pages/[tests]/**/*.vue'],
},
},
};
additionalConfig = getConfigFromName(configName, config);
}

export default defineNuxtConfig({
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/sample-project/src/pages/[...404].vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<template>
<span>Foo</span>
<span>404</span>
</template>
1 change: 1 addition & 0 deletions test/fixtures/sample-project/src/pages/[tests]/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Each file will be isolated type wise, so the e2e tests will navigate to the corresponding route
Empty file.
11 changes: 11 additions & 0 deletions test/unit/setupTypeTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { timeout } from '$$/utils';
import { $ } from 'zx';

export async function setupNuxtTestWithConfig(configName: string) {
try {
await $`NUXT_ROUTER_CONFIG_NAME=${configName} pnpm run test:types-fixtures`;
await timeout(100);
} catch (e) {
return Promise.reject(`Typecheck failed for config: [${configName}]: ${e}`);
}
}
15 changes: 15 additions & 0 deletions test/unit/unitTestAllConfigs.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import path from 'path';
import { setupNuxtTestWithConfig } from './setupTypeTest';
import { globby } from 'globby';

describe.sequential('Testing types with different configs', async () => {
try {
const allConfigs = await globby('test/samples-config', { absolute: false });
for (let config of allConfigs) {
const configFileName = path.parse(config).name;
await setupNuxtTestWithConfig(configFileName);
}
} catch (e: any) {
throw new Error(e);
}
});
24 changes: 22 additions & 2 deletions test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
export * from './tsd.utils';
export * from './typecheck';
import type { NuxtConfig } from '@nuxt/schema';
import { globbySync } from 'globby';

export function timeout(count: number) {
return new Promise((resolve) => setTimeout(resolve, count));
}

export function getConfigFromName(configName: string, config: NuxtConfig): NuxtConfig {
const files = globbySync(`test/fixtures/sample-project/tests/[${configName}]`);
return {
...config,
nuxtTypedRouter: {
...config.nuxtTypedRouter,
ignoreRoutes: ['[...404].vue', '[tests]/**/*.vue'],
},
typescript: {
tsConfig: {
files: [
...files.map((m) => m.replace('test/fixtures/sample-project', '..')),
`../src/pages/[tests]/[${configName}].vue`,
],
exclude: ['../tests/**/*', '../src/pages/[tests]/**/*.vue'],
},
},
};
}
21 changes: 0 additions & 21 deletions test/utils/tsd.utils.ts

This file was deleted.

8 changes: 0 additions & 8 deletions test/utils/typecheck.ts

This file was deleted.

12 changes: 11 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@ import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
globals: true,
testTimeout: 10000,
passWithNoTests: true,
pool: 'forks',
poolOptions: {
forks: {
singleFork: true,
},
},
sequence: {
concurrent: false,
hooks: 'list',
setupFiles: 'list',
},
},
resolve: {
alias: {
Expand Down

0 comments on commit fcc52d3

Please sign in to comment.