Skip to content

Commit

Permalink
allow overriding test config
Browse files Browse the repository at this point in the history
  • Loading branch information
haikyuu committed Apr 5, 2023
1 parent 57568f7 commit 9c23722
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
34 changes: 22 additions & 12 deletions packages/imba/bin/imba.config.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {defineConfig} from 'imba'
import { builtinModules } from 'module'
import imbaPlugin from 'imba/plugin'

import {mergeConfig} from 'vite'
import np from 'node:path'
import nfs from 'node:fs'
import url from 'node:url'

// uppercase letters + _
let envPrefix = ['_']
Expand All @@ -13,7 +14,9 @@ let envPrefix = ['_']
const extensions = ['.imba', '.imba1', '.mjs', '.js', '.ts', '.jsx', '.tsx', '.json']
const setupFiles = ['node_modules/imba/bin/test-setup.mjs']

export default defineConfig(()=>{
let userTestConfig = {}

export default defineConfig(async ({mode, command})=>{

extensions.forEach((ext)=>{
const name = `test-setup${ext}`
Expand All @@ -22,7 +25,23 @@ export default defineConfig(()=>{
setupFiles.push(path)
}
})
let finalTest = {test:{
globals: true,
include: ["**/*.{test,spec}.{imba,js,mjs,cjs,ts,mts,cts,jsx,tsx}"],
includeSource: ['**/*.{imba,js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
environment: "jsdom",
setupFiles,
exclude: ['node_modules']
// define: {
// 'import.meta.vitest': undefined,
// },
}}
if(mode == "test"){

/** REPLACE_ME */

finalTest = mergeConfig(finalTest, userTestConfig)
}
return ({
client: {
type: "",
Expand Down Expand Up @@ -86,15 +105,6 @@ export default defineConfig(()=>{
plugins: [imbaPlugin({ssr: true})],
envPrefix,
resolve: { extensions: ['.node.imba', ...extensions], dedupe: ['imba'] },
test: {
globals: true,
include: ["**/*.{test,spec}.{imba,js,mjs,cjs,ts,mts,cts,jsx,tsx}"],
includeSource: ['**/*.{imba,js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
environment: "jsdom",
setupFiles,
define: {
'import.meta.vitest': undefined,
},
}
test: finalTest.test
})
})
20 changes: 12 additions & 8 deletions packages/imba/bin/imba.imba
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,18 @@ def parseOptions options, extras = []
def test o
await ensurePackagesInstalled(['vitest', '@testing-library/dom', '@testing-library/jest-dom', 'jsdom'], process.cwd())
const vitest-path = np.join(process.cwd(), "node_modules/.bin", "vitest")

let testConfigPath = await getConfigFilePath("test", {mode: "development", command: "test"})

let configFile = testConfigPath

if testConfigPath == imbaConfigPath
# create a temporary file and put the config there
configFile = np.join process.cwd(), "node_modules", "imba.vitest.config.mjs"
const content = nfs.readFileSync(testConfigPath, 'utf-8')
nfs.writeFileSync(configFile, content)
# create a temporary file and put the config there
configFile = np.join process.cwd(), "node_modules", "imba.vitest.config.mjs"
let content = nfs.readFileSync(imbaConfigPath, 'utf-8')
if testConfigPath
content = content.replace '/** REPLACE_ME */', `
let userTestConfig = (await import(String(url.pathToFileURL('{testConfigPath}')))).default;
`
nfs.writeFileSync(configFile, content)

const params = ["--config", configFile, "--root", process.cwd(), "--dir", process.cwd(), ...o.args]
const options =
Expand All @@ -183,10 +185,12 @@ def test o
const vitest = spawn vitest-path, params, options

def run entry, o, extras
if entry.._name == 'preview' or (entry.._name == 'serve' and !o)
if entry.._name == 'preview' or entry.._name == 'serve'
# no args
let t = o
o = entry
entry = undefined
entry = t
entry = entry[0] if entry..length

unless o._name == 'preview' or o._name == 'serve' or o._name == 'build'
return cli.help! if o.args.length == 0
Expand Down
13 changes: 4 additions & 9 deletions packages/imba/src/utils/vite.imba
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,15 @@ export def getConfigFilePath(type, opts)
if nfs.existsSync path
let {default: imbaConfig} = await import(String url.pathToFileURL path)
if typeof imbaConfig == "function"
imbaConfig = imbaConfig({command, mode})

if imbaConfig[type]
if imbaConfig.plugins
return path
else
console.warn("You need to configure the plugins manually at the top level config or create a vitest.config.js file")
imbaConfig = await imbaConfig(opts)
return path
# otherwise use the default test config
return imbaConfigPath

# load default imba config
let {default: defaultImbaConfig} = await import(String url.pathToFileURL imbaConfigPath)
if typeof defaultImbaConfig == "function"
defaultImbaConfig = defaultImbaConfig({command, mode})
defaultImbaConfig = await defaultImbaConfig({command, mode})


const defaultConfig = defaultImbaConfig[type]
Expand All @@ -77,7 +72,7 @@ export def getConfigFilePath(type, opts)

let {default: imbaConfig} = await import(String url.pathToFileURL configPath)
if typeof imbaConfig == "function"
imbaConfig = imbaConfig({command, mode})
imbaConfig = await imbaConfig({command, mode})

return imbaConfig if type == "root"
const configObj = imbaConfig[type]
Expand Down

0 comments on commit 9c23722

Please sign in to comment.