Skip to content

Commit

Permalink
Add unit tests to verify support for function-wrapped exports
Browse files Browse the repository at this point in the history
  • Loading branch information
James M. Greene authored and James M. Greene committed Nov 18, 2022
1 parent d875fa8 commit d4a76d1
Showing 1 changed file with 71 additions and 3 deletions.
74 changes: 71 additions & 3 deletions src/config-parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const tempFolder = getTempFolder()
// Cases to test
const cases = [
//
// Default export
// Direct default export
//
{
property: 'property',
Expand Down Expand Up @@ -154,6 +154,73 @@ const cases = [
property: 'a.b.c',
source: 'var config = { a: { b: [], c: "hello" } }; module.exports = config',
expected: 'var config = { a: { b: { c: "value"}, c: "hello" } }; module.exports = config'
},

//
// Direct default export with wrapping call
//
{
property: 'property',
source: 'import { defineConfig } from "astro/config"; export default defineConfig({ p1: 0 })',
expected: 'import { defineConfig } from "astro/config"; export default defineConfig({ property: "value", p1: 0 })',
allowWrappingCall: true
},

//
// Direct module exports with wrapping call
//
{
property: 'property',
source: 'const { defineConfig } = require("astro/config"); module.exports = defineConfig({ p1: 0 })',
expected:
'const { defineConfig } = require("astro/config"); module.exports = defineConfig({ property: "value", p1: 0 })',
allowWrappingCall: true
},

//
// Indirect default export with wrapping call at the definition
//
{
property: 'property',
source: 'import { defineConfig } from "astro/config"; const config = defineConfig({}); export default config',
expected:
'import { defineConfig } from "astro/config"; const config = defineConfig({ property: "value" }); export default config',
allowWrappingCall: true
},

//
// Indirect default export with wrapping call at the export
//
{
property: 'property',
source: 'import { defineConfig } from "astro/config"; const config = {}; export default defineConfig(config)',
expected:
'import { defineConfig } from "astro/config"; const config = { property: "value" }; export default defineConfig(config)',
allowWrappingCall: true
},

//
// Indirect module exports with wrapping call at the definition
//
{
property: 'property',
source:
'const { defineConfig } = require("astro/config"); const config = defineConfig({}); module.exports = config',
expected:
'const { defineConfig } = require("astro/config"); const config = defineConfig({ property: "value"}); module.exports = config',
allowWrappingCall: true
},

//
// Indirect module exports with wrapping call at the export
//
{
property: 'property',
source:
'const { defineConfig } = require("astro/config"); const config = {}; module.exports = defineConfig(config)',
expected:
'const { defineConfig } = require("astro/config"); const config = { property: "value"}; module.exports = defineConfig(config)',
allowWrappingCall: true
}
]

Expand All @@ -168,7 +235,7 @@ describe('config-parser', () => {
jest.spyOn(core, 'debug').mockImplementation(jest.fn())
})

cases.forEach(({ property, source, expected }, index) => {
cases.forEach(({ property, source, expected, allowWrappingCall = false }, index) => {
it(`injects path properly for case #${index}`, () => {
// Write the source file
const sourceFile = `${tempFolder}/source.js`
Expand All @@ -180,7 +247,8 @@ describe('config-parser', () => {

// Update the settings and do the injection
new ConfigParser({
configurationFile: sourceFile
configurationFile: sourceFile,
allowWrappingCall
}).inject(property, 'value')

// Compare the files
Expand Down

0 comments on commit d4a76d1

Please sign in to comment.