-
Notifications
You must be signed in to change notification settings - Fork 922
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
svelte plugin: refactor tests, document new format, clean up config l…
…oading (#1304) * svelte plugin: refactor tests, clean up config option loading * Update README.md
- Loading branch information
1 parent
48ca18b
commit fe4bd1b
Showing
7 changed files
with
160 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
preprocess: { | ||
__test: 'custom-config.js::preprocess' | ||
}, | ||
compilerOptions: { | ||
__test: 'custom-config.js' | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
const path = require('path'); | ||
|
||
const mockCompiler = jest.fn().mockImplementation((code) => ({js: {code}})); | ||
const mockPreprocessor = jest.fn().mockImplementation((code) => code); | ||
jest.mock('svelte/compiler', () => ({compile: mockCompiler, preprocess: mockPreprocessor})); // important: mock before import | ||
|
||
const plugin = require('../plugin'); | ||
|
||
const mockConfig = {buildOptions: {sourceMaps: false}, installOptions: {rollup: {plugins: []}}}; | ||
const mockComponent = path.join(__dirname, 'Button.svelte'); | ||
|
||
describe('@snowpack/plugin-svelte (mocked)', () => { | ||
afterEach(() => { | ||
mockCompiler.mockClear(); | ||
mockPreprocessor.mockClear(); | ||
}); | ||
|
||
it('logs error if config options set but finds no file', async () => { | ||
expect(() => { | ||
plugin(mockConfig, { | ||
configFilePath: './plugins/plugin-svelte/this-file-does-not-exist.js', | ||
}); | ||
}).toThrow(/failed to find Svelte config file/); | ||
}); | ||
|
||
it('logs error if compileOptions is used instead of compilerOptions', async () => { | ||
expect(() => { | ||
plugin(mockConfig, { | ||
compileOptions: {__test: 'ignore'}, | ||
}); | ||
}).toThrow( | ||
`[plugin-svelte] Could not recognize "compileOptions". Did you mean "compilerOptions"?`, | ||
); | ||
}); | ||
|
||
it('logs error if old style config format is used', async () => { | ||
const badOptionCheck = /Svelte\.compile options moved to new config value/; | ||
expect(() => | ||
plugin(mockConfig, { | ||
css: false, | ||
}), | ||
).toThrow(badOptionCheck); | ||
expect(() => | ||
plugin(mockConfig, { | ||
generate: 'dom', | ||
}), | ||
).toThrow(badOptionCheck); | ||
}); | ||
|
||
it('passes compilerOptions to compiler', async () => { | ||
const compilerOptions = { | ||
__test: 'compilerOptions', | ||
}; | ||
const sveltePlugin = plugin(mockConfig, {compilerOptions}); | ||
await sveltePlugin.load({filePath: mockComponent}); | ||
expect(mockCompiler.mock.calls[0][1]).toEqual({ | ||
__test: 'compilerOptions', | ||
css: false, | ||
dev: true, | ||
filename: mockComponent, | ||
generate: 'dom', | ||
outputFilename: mockComponent, | ||
}); | ||
}); | ||
|
||
it('passes preprocess options to compiler', async () => { | ||
const preprocess = {__test: 'preprocess'}; | ||
const sveltePlugin = plugin(mockConfig, {preprocess}); | ||
await sveltePlugin.load({filePath: mockComponent}); | ||
expect(mockPreprocessor.mock.calls[0][1]).toEqual(preprocess); | ||
}); | ||
|
||
// For our users we load from the current working directory, but in jest that doesn't make sense | ||
it.skip('load config from a default svelte config file', async () => { | ||
const sveltePlugin = plugin(mockConfig, {}); | ||
await sveltePlugin.load({filePath: mockComponent}); | ||
expect(mockCompiler.mock.calls[0][1]).toEqual({__test: 'svelte.config.js'}); | ||
expect(mockPreprocessor.mock.calls[0][1]).toEqual({__test: 'svelte.config.js::preprocess'}); | ||
}); | ||
|
||
it('load config from a custom svelte config file', async () => { | ||
const sveltePlugin = plugin(mockConfig, { | ||
configFilePath: './plugins/plugin-svelte/test/custom-config.js', | ||
}); | ||
await sveltePlugin.load({filePath: mockComponent}); | ||
expect(mockCompiler.mock.calls[0][1]).toEqual({ | ||
__test: 'custom-config.js', | ||
css: false, | ||
dev: true, | ||
filename: mockComponent, | ||
generate: 'dom', | ||
outputFilename: mockComponent, | ||
}); | ||
expect(mockPreprocessor.mock.calls[0][1]).toEqual({__test: 'custom-config.js::preprocess'}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
const {sass} = require('svelte-preprocess-sass'); | ||
|
||
module.exports = { | ||
preprocess: { | ||
style: sass({}, {name: 'css'}), | ||
__test: 'svelte.config.js::preprocess' | ||
}, | ||
compilerOptions: { | ||
__test: 'svelte.config.js' | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
fe4bd1b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: