Skip to content

Commit

Permalink
feat(vite): add tests for nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Nov 8, 2023
1 parent 303393d commit fbe0e61
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 64 deletions.
96 changes: 96 additions & 0 deletions packages/vite/src/plugins/__snapshots__/plugin.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`@nx/vite/plugin root project should create nodes 1`] = `
{
"projects": {
".": {
"projectType": "library",
"root": ".",
"targets": {
"build": {
"cache": true,
"configurations": {
"development": {
"mode": "development",
},
"production": {
"mode": "production",
},
},
"defaultConfiguration": "production",
"executor": "@nx/vite:build",
"inputs": [
"default",
"^production",
],
"options": {
"outputPath": "dist",
},
"outputs": [
"{options.outputPath}",
],
},
"preview": {
"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",
},
},
"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",
},
},
"test": {
"cache": true,
"executor": "@nx/vite:test",
"inputs": [
"default",
"^production",
],
"options": {
"passWithNoTests": true,
"reportsDirectory": "coverage",
},
"outputs": [
"{options.reportsDirectory}",
],
},
},
},
},
}
`;
101 changes: 74 additions & 27 deletions packages/vite/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,91 @@
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;
describe('root project', () => {
beforeEach(async () => {
context = {
nxJsonConfiguration: {
namedInputs: {
default: ['{projectRoot}/**/*'],
production: ['!{projectRoot}/**/*.spec.ts'],
},
},
workspaceRoot: '',
};
});

beforeEach(async () => {
context = {
nxJsonConfiguration: {
namedInputs: {
default: ['{projectRoot}/**/*'],
production: ['!{projectRoot}/**/*.spec.ts'],
afterEach(() => {
jest.resetModules();
});

it('should create nodes', () => {
mockViteConfig('vite.config.ts', {});
const nodes = createNodesFunction(
'vite.config.ts',
{
buildTargetName: 'build',
serveTargetName: 'serve',
previewTargetName: 'preview',
testTargetName: 'test',
},
},
workspaceRoot: '',
};
});
context
);

afterEach(() => {
jest.resetModules();
expect(nodes).toMatchSnapshot();
});
});

it('should create nodes', () => {
mockViteConfig({});
const nodes = createNodesFunction(
'TODO',
{
targetName: 'target',
},
context
);

expect(nodes).toMatchInlineSnapshot();
// some issue wiht the tempfs
xdescribe('not root project', () => {
const tempFs = new TempFs('test');
beforeEach(() => {
context = {
nxJsonConfiguration: {
namedInputs: {
default: ['{projectRoot}/**/*'],
production: ['!{projectRoot}/**/*.spec.ts'],
},
},
workspaceRoot: tempFs.tempDir,
};

tempFs.createFileSync(
'my-app/project.json',
JSON.stringify({ name: 'my-app' })
);
tempFs.createFileSync('my-app/vite.config.ts', '');
});

afterEach(() => {
jest.resetModules();
});

it('should create nodes', () => {
mockViteConfig('my-app/vite.config.ts', {});
const nodes = createNodesFunction(
'my-app/vite.config.ts',
{
buildTargetName: 'build-something',
serveTargetName: 'my-serve',
previewTargetName: 'preview-site',
testTargetName: 'vitest',
},
context
);

expect(nodes).toMatchSnapshot();
});
});
});

function mockViteConfig(config: any) {
function mockViteConfig(configPath: string, config: UserConfig) {
jest.mock(
'TODO',
configPath,
() => ({
default: config,
}),
Expand Down
53 changes: 16 additions & 37 deletions packages/vite/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const createNodes: CreateNodes<VitePluginOptions> = [
'**/vite.config.{js,ts}',
(configFilePath, options, context) => {
const projectRoot = dirname(configFilePath);

console.log('projectRoot', projectRoot);
// Do not create a project if package.json and project.json isn't there.
const siblingFiles = readdirSync(projectRoot);
if (
Expand All @@ -48,8 +48,7 @@ export const createNodes: CreateNodes<VitePluginOptions> = [
configFilePath,
projectRoot,
options,
context,
projectName
context
),
},
},
Expand All @@ -61,8 +60,7 @@ function buildViteTargets(
configFilePath: string,
projectRoot: string,
options: VitePluginOptions,
context: CreateNodesContext,
projectName: string
context: CreateNodesContext
) {
// Is this needed for some reason?
getViteConfig(configFilePath, context);
Expand All @@ -77,25 +75,14 @@ function buildViteTargets(
projectRoot
);

targets[options.serveTargetName] = serveTarget(
context,
namedInputs,
projectRoot,
projectName
);
targets[options.serveTargetName] = serveTarget(context, namedInputs);

targets[options.previewTargetName] = previewTarget(
context,
namedInputs,
projectRoot,
projectName
);
targets[options.previewTargetName] = previewTarget(context, namedInputs);

targets[options.testTargetName] = testTarget(
context,
namedInputs,
projectRoot,
projectName
projectRoot
);

return targets;
Expand Down Expand Up @@ -148,9 +135,7 @@ function serveTarget(
context: CreateNodesContext,
namedInputs: {
[inputName: string]: any[];
},
projectRoot: string,
projectName: string
}
) {
const targetDefaults = readTargetDefaultsForTarget(
'serve',
Expand All @@ -162,16 +147,16 @@ function serveTarget(
executor: '@nx/vite:dev-server',
defaultConfiguration: 'development',
options: {
buildTarget: `${projectName}:build`,
buildTarget: `build`,
...targetDefaults?.options,
},
configurations: {
development: {
buildTarget: `${projectName}:build:development`,
buildTarget: `build:development`,
hmr: true,
},
production: {
buildTarget: `${projectName}:build:production`,
buildTarget: `build:production`,
hmr: false,
},
},
Expand All @@ -193,9 +178,7 @@ function previewTarget(
context: CreateNodesContext,
namedInputs: {
[inputName: string]: any[];
},
projectRoot: string,
projectName: string
}
) {
const targetDefaults = readTargetDefaultsForTarget(
'preview',
Expand All @@ -207,16 +190,16 @@ function previewTarget(
executor: '@nx/vite:preview-server',
defaultConfiguration: 'development',
options: {
buildTarget: `${projectName}:build`,
buildTarget: `build`,
...targetDefaults?.options,
},
configurations: {
development: {
buildTarget: `${projectName}:build:development`,
buildTarget: `build:development`,
hmr: true,
},
production: {
buildTarget: `${projectName}:build:production`,
buildTarget: `build:production`,
hmr: false,
},
},
Expand All @@ -239,19 +222,15 @@ function testTarget(
namedInputs: {
[inputName: string]: any[];
},
projectRoot: string,
projectName: string
projectRoot: string
) {
const targetDefaults = readTargetDefaultsForTarget(
'test',
context.nxJsonConfiguration.targetDefaults,
'@nx/vite:test'
);

const coveragePath = joinPathFragments(
'coverage',
projectRoot === '.' ? projectName : projectRoot
);
const coveragePath = joinPathFragments('coverage', projectRoot);

const baseTargetConfig: TargetConfiguration<ExecutorOptions> = {
executor: '@nx/vite:test',
Expand Down

0 comments on commit fbe0e61

Please sign in to comment.