Skip to content

Commit

Permalink
feat(vite): more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Nov 8, 2023
1 parent 276b959 commit 49ee5ae
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 19 deletions.
4 changes: 2 additions & 2 deletions docs/generated/packages/vite/generators/vitest.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
},
"coverageProvider": {
"type": "string",
"enum": ["v8", "c8", "custom"],
"default": "v8",
"enum": ["c8", "istanbul", "custom"],
"default": "c8",
"description": "Coverage provider to use."
},
"testTarget": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ export function replaceProjectConfigurationsWithPlugin<T = unknown>(

for (const [targetName, targetConfig] of Object.entries(node.targets)) {
const targetFromProjectConfig = projectConfig.targets[targetName];

if (targetFromProjectConfig.executor !== targetConfig.executor) {
if (targetFromProjectConfig?.executor !== targetConfig.executor) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/generators/vitest/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
},
"coverageProvider": {
"type": "string",
"enum": ["v8", "c8", "custom"],
"default": "v8",
"enum": ["c8", "istanbul", "custom"],
"default": "c8",
"description": "Coverage provider to use."
},
"testTarget": {
Expand Down
69 changes: 63 additions & 6 deletions packages/vite/src/migrations/update-17-2-0/add-vite-plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,74 @@
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { Tree } from '@nx/devkit';

import {
readProjectConfiguration,
Tree,
updateProjectConfiguration,
} from '@nx/devkit';
import update from './add-vite-plugin';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { UserConfig } from 'vite';

describe('add-vite-plugin migration', () => {
let tree: Tree;
let tempFs: TempFs;

function mockViteConfig(configPath: string, config: UserConfig) {
jest.mock(
configPath,
() => ({
default: config,
}),
{
virtual: true,
}
);
}

beforeEach(async () => {
tempFs = new TempFs('test');
tree = createTreeWithEmptyWorkspace();
tree.root = tempFs.tempDir;
await tempFs.createFiles({
'my-app/vite.config.ts': '',
'my-app/project.json': '{ "name": "my-app" }',
});
tree.write('my-app/vite.config.ts', `console.log('hi');`);
});

beforeEach(() => {
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
afterEach(() => {
jest.resetModules();
tempFs.cleanup();
});

it('should run successfully', async () => {
it('should remove the serve target', async () => {
mockViteConfig('my-app/vite.config.ts', {});
updateProjectConfiguration(tree, 'my-app', {
root: 'my-app',
targets: {
serve: {
executor: '@nx/vite:dev-server',
defaultConfiguration: 'development',
options: {
buildTarget: 'build',
},
configurations: {
development: {
buildTarget: 'build:development',
hmr: true,
},
production: {
buildTarget: 'build:production',
hmr: false,
},
},
},
},
});

await update(tree);
// ... expect changes made

expect(
readProjectConfiguration(tree, 'my-app').targets.serve
).toBeUndefined();
});
});
95 changes: 95 additions & 0 deletions packages/vite/src/plugins/__snapshots__/plugin.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,100 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`@nx/vite/plugin not root project should create nodes 1`] = `
{
"projects": {
"my-app": {
"projectType": "library",
"root": "my-app",
"targets": {
"build-something": {
"cache": true,
"configurations": {
"development": {
"mode": "development",
},
"production": {
"mode": "production",
},
},
"defaultConfiguration": "production",
"executor": "@nx/vite:build",
"inputs": [
"default",
"^production",
],
"options": {
"outputPath": "dist/my-app",
},
"outputs": [
"{options.outputPath}",
],
},
"my-serve": {
"cache": true,
"configurations": {
"development": {
"buildTarget": "build:development",
"hmr": true,
},
"production": {
"buildTarget": "build:production",
"hmr": false,
},
},
"defaultConfiguration": "development",
"executor": "@nx/vite:dev-server",
"inputs": [
"default",
"^production",
],
"options": {
"buildTarget": "build",
},
},
"preview-site": {
"cache": true,
"configurations": {
"development": {
"buildTarget": "build:development",
"hmr": true,
},
"production": {
"buildTarget": "build:production",
"hmr": false,
},
},
"defaultConfiguration": "development",
"executor": "@nx/vite:preview-server",
"inputs": [
"default",
"^production",
],
"options": {
"buildTarget": "build",
},
},
"vitest": {
"cache": true,
"executor": "@nx/vite:test",
"inputs": [
"default",
"^production",
],
"options": {
"passWithNoTests": true,
"reportsDirectory": "../coverage/my-app",
},
"outputs": [
"{options.reportsDirectory}",
],
},
},
},
},
}
`;

exports[`@nx/vite/plugin root project should create nodes 1`] = `
{
"projects": {
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CreateNodesContext } from '@nx/devkit';
import type { UserConfig } from 'vite';
import { createNodes } from './plugin';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { join } from 'path';

describe('@nx/vite/plugin', () => {
let createNodesFunction = createNodes[1];
let context: CreateNodesContext;
Expand Down Expand Up @@ -41,7 +41,7 @@ describe('@nx/vite/plugin', () => {
});

// some issue wiht the tempfs
xdescribe('not root project', () => {
describe('not root project', () => {
const tempFs = new TempFs('test');
beforeEach(() => {
context = {
Expand Down
12 changes: 7 additions & 5 deletions packages/vite/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const createNodes: CreateNodes<VitePluginOptions> = [
(configFilePath, options, context) => {
const projectRoot = dirname(configFilePath);
// Do not create a project if package.json and project.json isn't there.
const siblingFiles = readdirSync(projectRoot);
const siblingFiles = readdirSync(join(context.workspaceRoot, projectRoot));
if (
!siblingFiles.includes('package.json') &&
!siblingFiles.includes('project.json')
Expand Down Expand Up @@ -284,9 +284,11 @@ function getViteConfig(

function normalizeOptions(options: VitePluginOptions): VitePluginOptions {
options ??= {};
options.buildTargetName ??= 'build';
options.serveTargetName ??= 'serve';
options.previewTargetName ??= 'preview';
options.testTargetName ??= 'test';
// we do not want to infer these defaults, right?
// Maybe someone does not want to use the nodes for some reason?
// options.buildTargetName ??= 'build';
// options.serveTargetName ??= 'serve';
// options.previewTargetName ??= 'preview';
// options.testTargetName ??= 'test';
return options;
}

0 comments on commit 49ee5ae

Please sign in to comment.