Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24c852a
commit ddeff88
Showing
2 changed files
with
164 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,96 @@ | ||
import Fiber from 'fibers'; | ||
|
||
import loader from '../src/cjs'; | ||
import { | ||
getCompiler, | ||
compile, | ||
getTestId, | ||
getImplementationByName, | ||
} from './helpers/index'; | ||
|
||
beforeEach(() => { | ||
// The `sass` (`Dart Sass`) package modify the `Function` prototype, but the `jest` lose a prototype | ||
Object.setPrototypeOf(Fiber, Function.prototype); | ||
}); | ||
describe('validate options', () => { | ||
beforeEach(() => { | ||
// The `sass` (`Dart Sass`) package modify the `Function` prototype, but the `jest` lose a prototype | ||
Object.setPrototypeOf(Fiber, Function.prototype); | ||
}); | ||
|
||
it('validate options', () => { | ||
const validate = (options) => | ||
loader.call( | ||
Object.assign( | ||
{}, | ||
{ | ||
query: options, | ||
loaders: [], | ||
resourcePath: 'file.scss', | ||
getResolve: () => () => {}, | ||
async: () => (error) => { | ||
if (error) { | ||
throw error; | ||
} | ||
}, | ||
} | ||
), | ||
'a { color: red; }' | ||
); | ||
const tests = { | ||
implementation: { | ||
// eslint-disable-next-line global-require | ||
success: [require('sass'), require('node-sass')], | ||
failure: [true, 'string'], | ||
}, | ||
sassOptions: { | ||
success: [{}, { indentWidth: 6 }, () => ({ indentWidth: 6 })], | ||
failure: [true, 'string'], | ||
}, | ||
prependData: { | ||
success: ['$color: red;', () => '$color: red;'], | ||
failure: [true], | ||
}, | ||
sourceMap: { | ||
success: [true, false], | ||
failure: ['string'], | ||
}, | ||
webpackImporter: { | ||
success: [true, false], | ||
failure: ['string'], | ||
}, | ||
unknown: { | ||
success: [], | ||
failure: [1, true, false, 'test', /test/, [], {}, { foo: 'bar' }], | ||
}, | ||
}; | ||
|
||
expect(() => | ||
// eslint-disable-next-line global-require | ||
validate({ implementation: require('node-sass') }) | ||
).not.toThrow(); | ||
expect(() => | ||
// eslint-disable-next-line global-require | ||
validate({ implementation: require('sass') }) | ||
).not.toThrow(); | ||
expect(() => | ||
validate({ implementation: true }) | ||
).toThrowErrorMatchingSnapshot(); | ||
function stringifyValue(value) { | ||
if ( | ||
Array.isArray(value) || | ||
(value && typeof value === 'object' && value.constructor === Object) | ||
) { | ||
return JSON.stringify(value); | ||
} | ||
|
||
expect(() => validate({ sassOptions: {} })).not.toThrow(); | ||
expect(() => | ||
validate({ | ||
sassOptions: () => { | ||
return {}; | ||
}, | ||
}) | ||
).not.toThrow(); | ||
expect(() => validate({ sassOptions: () => {} })).not.toThrow(); | ||
expect(() => validate({ sassOptions: true })).toThrowErrorMatchingSnapshot(); | ||
expect(() => | ||
validate({ sassOptions: { indentWidth: 6, linefeed: 'crlf' } }) | ||
).not.toThrow(); | ||
return value; | ||
} | ||
|
||
expect(() => validate({ prependData: '$color: red;' })).not.toThrow(); | ||
expect(() => validate({ prependData: () => '$color: red;' })).not.toThrow(); | ||
expect(() => validate({ prependData: true })).toThrowErrorMatchingSnapshot(); | ||
async function createTestCase(key, value, type) { | ||
it(`should ${ | ||
type === 'success' ? 'successfully validate' : 'throw an error on' | ||
} the "${key}" option with "${stringifyValue(value)}" value`, async () => { | ||
const testId = getTestId('language', 'scss'); | ||
const compiler = getCompiler(testId, { | ||
loader: { | ||
options: { | ||
implementation: getImplementationByName('dart-sass'), | ||
[key]: value, | ||
}, | ||
}, | ||
}); | ||
let stats; | ||
|
||
try { | ||
stats = await compile(compiler); | ||
} finally { | ||
if (type === 'success') { | ||
expect(stats.hasErrors()).toBe(false); | ||
} else if (type === 'failure') { | ||
const { | ||
compilation: { errors }, | ||
} = stats; | ||
|
||
expect(() => validate({ webpackImporter: true })).not.toThrow(); | ||
expect(() => validate({ webpackImporter: false })).not.toThrow(); | ||
expect(() => | ||
validate({ webpackImporter: 'unknown' }) | ||
).toThrowErrorMatchingSnapshot(); | ||
expect(errors).toHaveLength(1); | ||
expect(() => { | ||
throw new Error(errors[0].error.message); | ||
}).toThrowErrorMatchingSnapshot(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
expect(() => validate({ unknown: 'unknown' })).toThrowErrorMatchingSnapshot(); | ||
for (const [key, values] of Object.entries(tests)) { | ||
for (const type of Object.keys(values)) { | ||
for (const value of values[type]) { | ||
createTestCase(key, value, type); | ||
} | ||
} | ||
} | ||
}); |