Skip to content

chore: add validation that expected method is called #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/__tests__/transformer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const __dirname = dirname(fileURLToPath(import.meta.url))
const runTransformer = async (filename, options) => {
const path = `${__dirname}/fixtures/${filename}.svelte`
const source = readFileSync(path).toString()
const result = await transformer.processAsync(source, path, { transformerConfig: options })
const result = await transformer.processAsync(source, path, { extensionsToTreatAsEsm: '.svelte', transformerConfig: options })
expect(result.code).toBeDefined()
expect(result.code).toContain('SvelteComponent')
expect(result.map).toBeDefined()
Expand Down
28 changes: 18 additions & 10 deletions src/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import { getSvelteConfig } from './svelteconfig.js'
const dynamicImport = async (filename) => import(pathToFileURL(filename).toString())

/**
* Jest will only call this method when running in ESM mode.
* Jest will only call this method when running tests in ESM mode.
*/
const processAsync = async (source, filename, jestOptions) => {
const options = jestOptions && jestOptions.transformerConfig ? jestOptions.transformerConfig : {}
const { preprocess, rootMode } = options
const format = (jestOptions?.extensionsToTreatAsEsm || []).includes('.svelte') ? 'esm' : 'cjs'
if (format !== 'esm') {
throw new Error("jest is calling svelte-jester's processAsync method, but .svelte was not found in extensionsToTreatAsEsm. This is unexpected.")
}
const compilerOptions = jestOptions?.transformerConfig || {}
const { preprocess, rootMode } = compilerOptions

if (!preprocess) {
return compiler('esm', options, filename, source)
return compiler(format, compilerOptions, filename, source)
}

const svelteConfigPath = getSvelteConfig(rootMode, filename, preprocess)
Expand All @@ -26,18 +30,22 @@ const processAsync = async (source, filename, jestOptions) => {
{ filename }
)

return compiler('esm', options, filename, processed.code, processed.map)
return compiler(format, compilerOptions, filename, processed.code, processed.map)
}

/**
* Starts a new process, so is higher overhead than processAsync.
* However, Jest calls this method in CJS mode.
* Jest calls this method when running tests in CJS mode.
*/
const processSync = (source, filename, jestOptions) => {
const options = jestOptions && jestOptions.transformerConfig ? jestOptions.transformerConfig : {}
const { preprocess, rootMode, maxBuffer, showConsoleLog } = options
const format = (jestOptions?.extensionsToTreatAsEsm || []).includes('.svelte') ? 'esm' : 'cjs'
if (format !== 'cjs') {
throw new Error("jest is calling svelte-jester's processSync method, but .svelte was found in extensionsToTreatAsEsm. This is unexpected.")
}
const compilerOptions = jestOptions?.transformerConfig || {}
const { preprocess, rootMode, maxBuffer, showConsoleLog } = compilerOptions
if (!preprocess) {
return compiler('cjs', options, filename, source)
return compiler(format, compilerOptions, filename, source)
}

const svelteConfig = getSvelteConfig(rootMode, filename, preprocess)
Expand All @@ -52,7 +60,7 @@ const processSync = (source, filename, jestOptions) => {
).toString()

const parsedPreprocessResult = JSON.parse(preprocessResult)
return compiler('cjs', options, filename, parsedPreprocessResult.code, parsedPreprocessResult.map)
return compiler(format, compilerOptions, filename, parsedPreprocessResult.code, parsedPreprocessResult.map)
}

const compiler = (format, options = {}, filename, processedCode, processedMap) => {
Expand Down