|
1 | 1 | import { process } from '../../src/preprocessor'; |
2 | 2 |
|
| 3 | +const path = '/path/to/file.html'; |
| 4 | +const config = { globals: {} as any }; |
| 5 | +// wrap a transformed source so that we can fake a `require()` on it by calling the returned wrapper |
| 6 | +const wrap = (src: string) => |
| 7 | + new Function(`var module={}; ${src} return module.exports;`) as any; |
| 8 | + |
3 | 9 | const source = `<div class="html-test"> |
4 | 10 | <span class="html-test__element">This is element</span> |
| 11 | + <code>This is a backtilt \`</code> |
5 | 12 | </div>`; |
6 | | -const path = '/path/to/file.html'; |
7 | | -const config = { |
8 | | - globals: { |
9 | | - __TRANSFORM_HTML__: true, |
10 | | - }, |
11 | | -}; |
12 | 13 |
|
13 | 14 | describe('Html transforms', () => { |
14 | 15 | it('transforms html if config.globals.__TRANSFORM_HTML__ is set', () => { |
15 | | - expect(process(source, path, config)).toMatchSnapshot(); |
16 | | - delete config.globals.__TRANSFORM_HTML__; |
17 | | - expect(process(source, path, config)).toMatchSnapshot(); |
| 16 | + // get the untransformed version |
| 17 | + const untransformed = process(source, path, config); |
| 18 | + // ... then the one which should be transformed |
| 19 | + config.globals.__TRANSFORM_HTML__ = true; |
| 20 | + const transformed = process(source, path, config) as string; |
| 21 | + // ... finally the result of a `require('module-with-transformed-version')` |
| 22 | + const exported = wrap(transformed)(); |
| 23 | + |
| 24 | + expect(exported).toMatchSnapshot('module'); |
| 25 | + expect(transformed).toMatchSnapshot('source'); |
| 26 | + expect(untransformed).toMatchSnapshot('untransformed'); |
| 27 | + // requiring the transformed version should return the same string as the untransformed version |
| 28 | + expect(exported).toBe(untransformed); |
18 | 29 | }); |
19 | 30 | }); |
0 commit comments