Skip to content
This repository was archived by the owner on Oct 29, 2021. It is now read-only.

Commit 501fd42

Browse files
committed
Make tests more stable by checking export value
1 parent ef1150b commit 501fd42

File tree

2 files changed

+72
-99
lines changed

2 files changed

+72
-99
lines changed
Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`can export only locals 1`] = `
4-
"// Exports
5-
module.exports = {
6-
\\"text\\": \\"_3KySKA6rf07sl0ilq7bBBm\\"
7-
};
8-
9-
module.exports = module.exports || {};
10-
module.exports[\\"customMedia\\"] = {
11-
\\"--narrow-window\\": \\"(max-width: 30em)\\"
12-
};
13-
module.exports[\\"customProperties\\"] = {
14-
\\"--primary-color\\": \\"lightblue\\"
15-
};
16-
module.exports[\\"customSelectors\\"] = {
17-
\\":--title\\": \\"h1\\"
18-
};"
19-
`;
20-
213
exports[`emits an error when css-customs-loader is placed after css-loader 1`] = `
224
"./fixtures/basic.css
235
Module build failed (from ../index.js):
@@ -61,67 +43,3 @@ postcss-preset-env is missing from your plugins, you need to use it in order for
6143
at Runtime.requireModule (<CWD>/node_modules/jest-runtime/build/index.js:536:10)
6244
at Runtime.requireModuleOrMock (<CWD>/node_modules/jest-runtime/build/index.js:699:21)"
6345
`;
64-
65-
exports[`exposes CSS Modules in the same object as customs 1`] = `
66-
"exports = module.exports = require(\\"<relative path to CWD>/node_modules/css-loader/dist/runtime/api.js\\")(false);
67-
// Module
68-
exports.push([module.id, \\":root {\\\\n --primary-color: lightblue;\\\\n}\\\\n\\\\n._3KySKA6rf07sl0ilq7bBBm {\\\\n color: lightblue;\\\\n color: var(--primary-color);\\\\n}\\\\n\\", \\"\\"]);
69-
// Exports
70-
exports.locals = {
71-
\\"text\\": \\"_3KySKA6rf07sl0ilq7bBBm\\"
72-
};
73-
74-
exports.locals = exports.locals || {};
75-
exports.locals[\\"customMedia\\"] = {
76-
\\"--narrow-window\\": \\"(max-width: 30em)\\"
77-
};
78-
exports.locals[\\"customProperties\\"] = {
79-
\\"--primary-color\\": \\"lightblue\\"
80-
};
81-
exports.locals[\\"customSelectors\\"] = {
82-
\\":--title\\": \\"h1\\"
83-
};"
84-
`;
85-
86-
exports[`exposes CSS customs in the default export object 1`] = `
87-
"exports = module.exports = require(\\"<relative path to CWD>/node_modules/css-loader/dist/runtime/api.js\\")(false);
88-
// Module
89-
exports.push([module.id, \\":root {\\\\n --primary-color: lightblue;\\\\n}\\\\n\\", \\"\\"]);
90-
91-
exports.locals = exports.locals || {};
92-
exports.locals[\\"customMedia\\"] = {
93-
\\"--narrow-window\\": \\"(max-width: 30em)\\"
94-
};
95-
exports.locals[\\"customProperties\\"] = {
96-
\\"--primary-color\\": \\"lightblue\\"
97-
};
98-
exports.locals[\\"customSelectors\\"] = {
99-
\\":--title\\": \\"h1\\"
100-
};"
101-
`;
102-
103-
exports[`uses PostCSS plugins before postcss-preset-env 1`] = `
104-
"exports = module.exports = require(\\"<relative path to CWD>/node_modules/css-loader/dist/runtime/api.js\\")(false);
105-
// Module
106-
exports.push([module.id, \\":root {\\\\n --primary-color: #1da1f2;\\\\n}\\\\n\\", \\"\\"]);
107-
108-
exports.locals = exports.locals || {};
109-
exports.locals[\\"customMedia\\"] = {};
110-
exports.locals[\\"customProperties\\"] = {
111-
\\"--primary-color\\": \\"#1da1f2\\"
112-
};
113-
exports.locals[\\"customSelectors\\"] = {};"
114-
`;
115-
116-
exports[`uses webpack loaders after postcss-loader 1`] = `
117-
"exports = module.exports = require(\\"<relative path to CWD>/node_modules/css-loader/dist/runtime/api.js\\")(false);
118-
// Module
119-
exports.push([module.id, \\":root {\\\\n --primary-color: lightblue;\\\\n}\\\\n.text {\\\\n color: lightblue;\\\\n color: var(--primary-color);\\\\n}\\\\n\\", \\"\\"]);
120-
121-
exports.locals = exports.locals || {};
122-
exports.locals[\\"customMedia\\"] = {};
123-
exports.locals[\\"customProperties\\"] = {
124-
\\"--primary-color\\": \\"lightblue\\"
125-
};
126-
exports.locals[\\"customSelectors\\"] = {};"
127-
`;

packages/css-customs-loader/test/index.test.js

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1+
const requireFromString = require('require-from-string')
12
const path = require('path')
23
const compile = require('./compile')
34

45
const cssCustomsLoader = require.resolve('../')
5-
const getCompiledOutput = ({ stats, entry }) =>
6-
stats
6+
const getCompiledExportedValue = ({ stats, entry }) => {
7+
const { source } = stats
78
.toJson()
89
.modules.find(
910
({ name, identifier }) =>
1011
name.includes(entry) && identifier.includes(cssCustomsLoader)
1112
)
12-
// normalize for CI
13-
.source.replace(
14-
path.relative(`${__dirname}/fixtures`, process.cwd()),
15-
'<relative path to CWD>'
16-
)
13+
return requireFromString(
14+
source,
15+
`${__dirname}/fixtures/${path.basename(entry)}`
16+
)
17+
}
1718

1819
describe(`emits an error`, () => {
1920
test(`when css-customs-loader is placed after css-loader`, async () => {
@@ -101,8 +102,20 @@ it(`exposes CSS customs in the default export object`, async () => {
101102
},
102103
],
103104
})
104-
const output = getCompiledOutput({ stats, entry })
105-
expect(output).toMatchSnapshot()
105+
const { locals } = getCompiledExportedValue({ stats, entry })
106+
expect(locals).toMatchInlineSnapshot(`
107+
Object {
108+
"customMedia": Object {
109+
"--narrow-window": "(max-width: 30em)",
110+
},
111+
"customProperties": Object {
112+
"--primary-color": "lightblue",
113+
},
114+
"customSelectors": Object {
115+
":--title": "h1",
116+
},
117+
}
118+
`)
106119
})
107120

108121
it(`exposes CSS Modules in the same object as customs`, async () => {
@@ -125,8 +138,21 @@ it(`exposes CSS Modules in the same object as customs`, async () => {
125138
},
126139
],
127140
})
128-
const output = getCompiledOutput({ stats, entry })
129-
expect(output).toMatchSnapshot()
141+
const { locals } = getCompiledExportedValue({ stats, entry })
142+
expect(locals).toMatchInlineSnapshot(`
143+
Object {
144+
"customMedia": Object {
145+
"--narrow-window": "(max-width: 30em)",
146+
},
147+
"customProperties": Object {
148+
"--primary-color": "lightblue",
149+
},
150+
"customSelectors": Object {
151+
":--title": "h1",
152+
},
153+
"text": "_3KySKA6rf07sl0ilq7bBBm",
154+
}
155+
`)
130156
})
131157

132158
it(`can export only locals`, async () => {
@@ -150,8 +176,21 @@ it(`can export only locals`, async () => {
150176
},
151177
],
152178
})
153-
const output = getCompiledOutput({ stats, entry })
154-
expect(output).toMatchSnapshot()
179+
const result = getCompiledExportedValue({ stats, entry })
180+
expect(result).toMatchInlineSnapshot(`
181+
Object {
182+
"customMedia": Object {
183+
"--narrow-window": "(max-width: 30em)",
184+
},
185+
"customProperties": Object {
186+
"--primary-color": "lightblue",
187+
},
188+
"customSelectors": Object {
189+
":--title": "h1",
190+
},
191+
"text": "_3KySKA6rf07sl0ilq7bBBm",
192+
}
193+
`)
155194
})
156195

157196
it(`supports files with external @imports`, async () => {
@@ -210,8 +249,16 @@ it(`uses PostCSS plugins before postcss-preset-env`, async () => {
210249
},
211250
],
212251
})
213-
const output = getCompiledOutput({ stats, entry })
214-
expect(output).toMatchSnapshot()
252+
const { locals } = getCompiledExportedValue({ stats, entry })
253+
expect(locals).toMatchInlineSnapshot(`
254+
Object {
255+
"customMedia": Object {},
256+
"customProperties": Object {
257+
"--primary-color": "#1da1f2",
258+
},
259+
"customSelectors": Object {},
260+
}
261+
`)
215262
})
216263

217264
it('uses webpack loaders after postcss-loader', async () => {
@@ -234,6 +281,14 @@ it('uses webpack loaders after postcss-loader', async () => {
234281
},
235282
],
236283
})
237-
const output = getCompiledOutput({ stats, entry })
238-
expect(output).toMatchSnapshot()
284+
const { locals } = getCompiledExportedValue({ stats, entry })
285+
expect(locals).toMatchInlineSnapshot(`
286+
Object {
287+
"customMedia": Object {},
288+
"customProperties": Object {
289+
"--primary-color": "lightblue",
290+
},
291+
"customSelectors": Object {},
292+
}
293+
`)
239294
})

0 commit comments

Comments
 (0)