Skip to content

Commit

Permalink
test: add test for conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jan 17, 2024
1 parent 7700adb commit e4f323a
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 2 deletions.
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ packages:
- packages/*
- examples/*
- test/*
- test/config/fixtures/conditions-pkg
1 change: 1 addition & 0 deletions test/config/fixtures/conditions-pkg/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as condition } from 'subpackage'
9 changes: 9 additions & 0 deletions test/config/fixtures/conditions-pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "conditions",
"private": true,
"type": "module",
"main": "index.js",
"dependencies": {
"subpackage": "file:../conditions-subpackage"
}
}
1 change: 1 addition & 0 deletions test/config/fixtures/conditions-subpackage/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'custom'
1 change: 1 addition & 0 deletions test/config/fixtures/conditions-subpackage/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('Should not be imported')
1 change: 1 addition & 0 deletions test/config/fixtures/conditions-subpackage/development.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'development'
13 changes: 13 additions & 0 deletions test/config/fixtures/conditions-subpackage/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "subpackage",
"private": true,
"type": "module",
"exports": {
".": {
"custom": "./custom.js",
"development": "./development.js",
"production": "./production.js",
"default": "./default.js"
}
}
}
1 change: 1 addition & 0 deletions test/config/fixtures/conditions-subpackage/production.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'production'
6 changes: 6 additions & 0 deletions test/config/fixtures/conditions-test/conditions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { test, expect } from 'vitest';
import { condition } from '../conditions-pkg';

test('condition is correct', () => {
expect(condition).toBe(TEST_CONDITION)
})
3 changes: 3 additions & 0 deletions test/config/fixtures/conditions-test/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({})
62 changes: 62 additions & 0 deletions test/config/test/conditions-cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { expect, test } from 'vitest'
import { runVitest } from '../../test-utils'

test('correctly imports external dependencies with a development condition', async () => {
// dev condition is the default
const { stderr } = await runVitest({
root: 'fixtures/conditions-test',
server: {
deps: {
external: [/conditions-pkg/],
},
},
}, [], 'test', {
define: {
TEST_CONDITION: '"development"',
},
})

expect(stderr).toBe('')
})

test('correctly imports external dependencies with a production condition', async () => {
// this is the only check in Vite for "isProduction" value
process.env.NODE_ENV = 'production'

const { stderr } = await runVitest({
root: 'fixtures/conditions-test',
server: {
deps: {
external: [/conditions-pkg/],
},
},
}, [], 'test', {
define: {
TEST_CONDITION: '"production"',
},
})

expect(stderr).toBe('')
})

test('correctly imports external dependencies with a custom condition', async () => {
delete process.env.NODE_ENV

const { stderr } = await runVitest({
root: 'fixtures/conditions-test',
server: {
deps: {
external: [/conditions-pkg/],
},
},
}, [], 'test', {
resolve: {
conditions: ['custom'],
},
define: {
TEST_CONDITION: '"custom"',
},
})

expect(stderr).toBe('')
})
5 changes: 3 additions & 2 deletions test/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { Console } from 'node:console'
import { Writable } from 'node:stream'
import fs from 'node:fs'
import { fileURLToPath } from 'node:url'
import type { UserConfig as ViteUserConfig } from 'vite'
import { type UserConfig, type VitestRunMode, afterEach } from 'vitest'
import type { Vitest } from 'vitest/node'
import { startVitest } from 'vitest/node'
import { type Options, execa } from 'execa'
import stripAnsi from 'strip-ansi'
import { dirname, resolve } from 'pathe'

export async function runVitest(config: UserConfig, cliFilters: string[] = [], mode: VitestRunMode = 'test') {
export async function runVitest(config: UserConfig, cliFilters: string[] = [], mode: VitestRunMode = 'test', viteOverrides: ViteUserConfig = {}) {
// Reset possible previous runs
process.exitCode = 0
let exitCode = process.exitCode
Expand All @@ -26,7 +27,7 @@ export async function runVitest(config: UserConfig, cliFilters: string[] = [], m
watch: false,
reporters: ['verbose'],
...config,
})
}, viteOverrides)
}
catch (e: any) {
return {
Expand Down

0 comments on commit e4f323a

Please sign in to comment.