-
Notifications
You must be signed in to change notification settings - Fork 916
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Svelte plugin SSR bug; add test (#1241)
- Loading branch information
Showing
7 changed files
with
287 additions
and
16 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 @@ | ||
<style lang="scss"> | ||
button { | ||
color: rgb(blue, 0.7); | ||
font-size: 16px; | ||
} | ||
</style> | ||
|
||
<button>I’m a button!</button> |
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,43 @@ | ||
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('passes options to compiler', async () => { | ||
const options = { | ||
generate: 'ssr', | ||
isDev: false, | ||
}; | ||
const sveltePlugin = plugin(mockConfig, options); | ||
await sveltePlugin.load({filePath: mockComponent}); | ||
const passedOptions = mockCompiler.mock.calls[0][1]; | ||
|
||
// this tests that all options passed above made it to the compiler | ||
// objectContaining() allows additional options to be passed, but we only care that our options have been preserved | ||
expect(passedOptions).toEqual(expect.objectContaining(options)); | ||
}); | ||
|
||
it('handles preprocessing', async () => { | ||
// test-only config option | ||
const options = {__config: path.join(__dirname, 'svelte.config.js')}; | ||
|
||
const sveltePlugin = plugin(mockConfig, options); | ||
|
||
await sveltePlugin.load({filePath: mockComponent}); | ||
|
||
// as long as this function has been called, we can assume success | ||
expect(mockPreprocessor).toHaveBeenCalled(); | ||
}); | ||
}); |
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,7 @@ | ||
const {sass} = require('svelte-preprocess-sass'); | ||
|
||
module.exports = { | ||
preprocess: { | ||
style: sass({}, {name: 'css'}), | ||
}, | ||
}; |
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,17 @@ | ||
const path = require('path'); | ||
const plugin = require('../plugin'); | ||
|
||
const mockConfig = {buildOptions: {sourceMaps: false}, installOptions: {rollup: {plugins: []}}}; | ||
const mockComponent = path.join(__dirname, 'Button.svelte'); | ||
|
||
describe('@snowpack/plugin-svelte (unmocked)', () => { | ||
it('generates code', async () => { | ||
const options = {}; | ||
const sveltePlugin = plugin(mockConfig, options); | ||
const result = await sveltePlugin.load({filePath: mockComponent}); | ||
|
||
// assume if some CSS & JS were returned, it transformed successfully | ||
expect(result['.css'].code).toBeTruthy(); | ||
expect(result['.js'].code).toBeTruthy(); | ||
}); | ||
}); |
Oops, something went wrong.
1faf063
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: