Skip to content

Commit c36b66e

Browse files
committed
Refactor tests: use ava macros
1 parent 7b24d7c commit c36b66e

File tree

10 files changed

+167
-205
lines changed

10 files changed

+167
-205
lines changed

test/custom-load.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,12 @@
22
import test from "ava"
33

44
// internal tooling
5-
import compareFixtures from "./helpers/compare-fixtures"
5+
import checkFixture from "./helpers/check-fixture"
66

7-
test.serial("should accept content", t => {
8-
return compareFixtures(t, "custom-load", {
9-
load: () => {
10-
return "custom-content {}"
11-
},
12-
})
7+
test.serial("should accept content", checkFixture, "custom-load", {
8+
load: () => "custom-content {}",
139
})
1410

15-
test.serial("should accept promised content", t => {
16-
return compareFixtures(t, "custom-load", {
17-
load: () => {
18-
return Promise.resolve("custom-content {}")
19-
},
20-
})
11+
test.serial("should accept promised content", checkFixture, "custom-load", {
12+
load: () => Promise.resolve("custom-content {}"),
2113
})

test/custom-resolve.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,57 @@ import test from "ava"
66
import postcss from "postcss"
77

88
// internal tooling
9-
import compareFixtures from "./helpers/compare-fixtures"
9+
import checkFixture from "./helpers/check-fixture"
1010

1111
// plugin
1212
import atImport from ".."
1313

14-
test.serial("should accept file", t => {
15-
return compareFixtures(t, "custom-resolve-file", {
16-
resolve: () => path.resolve("test/fixtures/imports/custom-resolve-1.css"),
17-
})
14+
test.serial("should accept file", checkFixture, "custom-resolve-file", {
15+
resolve: () => path.resolve("test/fixtures/imports/custom-resolve-1.css"),
1816
})
1917

20-
test.serial("should accept promised file", t => {
21-
return compareFixtures(t, "custom-resolve-file", {
18+
test.serial(
19+
"should accept promised file",
20+
checkFixture,
21+
"custom-resolve-file",
22+
{
2223
resolve: () => {
2324
return Promise.resolve(
2425
path.resolve("test/fixtures/imports/custom-resolve-1.css")
2526
)
2627
},
27-
})
28-
})
28+
}
29+
)
2930

30-
test.serial("should accept array of files", t => {
31-
return compareFixtures(t, "custom-resolve-array", {
31+
test.serial(
32+
"should accept array of files",
33+
checkFixture,
34+
"custom-resolve-array",
35+
{
3236
resolve: () => {
3337
return [
3438
path.resolve("test/fixtures/imports/custom-resolve-1.css"),
3539
path.resolve("test/fixtures/imports/custom-resolve-2.css"),
3640
path.resolve("test/fixtures/imports/custom-resolve-1.css"),
3741
]
3842
},
39-
})
40-
})
43+
}
44+
)
4145

42-
test.serial("should accept promised array of files", t => {
43-
return compareFixtures(t, "custom-resolve-array", {
46+
test.serial(
47+
"should accept promised array of files",
48+
checkFixture,
49+
"custom-resolve-array",
50+
{
4451
resolve: () => {
4552
return Promise.resolve([
4653
path.resolve("test/fixtures/imports/custom-resolve-1.css"),
4754
path.resolve("test/fixtures/imports/custom-resolve-2.css"),
4855
path.resolve("test/fixtures/imports/custom-resolve-1.css"),
4956
])
5057
},
51-
})
52-
})
58+
}
59+
)
5360

5461
test("should apply default resolver when custom doesn't return an absolute path", t => {
5562
return postcss()

test/custom-syntax-parser.js

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,48 @@ import scss from "postcss-scss"
44
import sugarss from "sugarss"
55

66
// internal tooling
7-
import compareFixtures from "./helpers/compare-fixtures"
8-
import compareFixturesExt from "./helpers/compare-fixtures-ext"
7+
import checkFixture from "./helpers/check-fixture"
98

10-
test("should process custom syntax", t => {
11-
return compareFixtures(t, "scss-syntax", null, { syntax: scss })
9+
test("should process custom syntax", checkFixture, "scss-syntax", null, {
10+
syntax: scss,
1211
})
1312

14-
test("should process custom syntax by parser", t => {
15-
return compareFixtures(t, "scss-parser", null, { parser: scss })
16-
})
17-
18-
test(".css importing .sss should work", t => {
19-
return compareFixtures(t, "import-sss")
20-
})
21-
22-
test(".sss importing .sss should work", t => {
23-
return compareFixturesExt(t, "sugar", ".sss", null, { parser: sugarss })
24-
})
25-
26-
test(".sss importing .css should work", t => {
27-
return compareFixturesExt(t, "sugar-import-css", ".sss", null, {
28-
parser: sugarss,
29-
})
30-
})
31-
32-
test(".css importing .sss importing .css should work", t => {
33-
return compareFixtures(t, "import-sss-css")
34-
})
35-
36-
test(".sss importing .css importing .sss should work", t => {
37-
return compareFixturesExt(t, "import-css-sss", ".sss", null, {
38-
parser: sugarss,
39-
})
40-
})
13+
test(
14+
"should process custom syntax by parser",
15+
checkFixture,
16+
"scss-parser",
17+
null,
18+
{ parser: scss }
19+
)
20+
21+
test(".css importing .sss should work", checkFixture, "import-sss")
22+
23+
test(
24+
".sss importing .sss should work",
25+
checkFixture,
26+
{ name: "sugar", ext: ".sss" },
27+
null,
28+
{ parser: sugarss }
29+
)
30+
31+
test(
32+
".sss importing .css should work",
33+
checkFixture,
34+
{ name: "sugar-import-css", ext: ".sss" },
35+
null,
36+
{ parser: sugarss }
37+
)
38+
39+
test(
40+
".css importing .sss importing .css should work",
41+
checkFixture,
42+
"import-sss-css"
43+
)
44+
45+
test(
46+
".sss importing .css importing .sss should work",
47+
checkFixture,
48+
{ name: "import-css-sss", ext: ".sss" },
49+
null,
50+
{ parser: sugarss }
51+
)
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,24 @@ const postcss = require("postcss")
1010
const atImport = require("../..")
1111

1212
function read(name, ext) {
13-
if (!ext) ext = ".css"
13+
ext = ext || ".css"
1414
return fs.readFileSync(`test/fixtures/${name}${ext}`, "utf8")
1515
}
1616

17-
module.exports = function(t, name, ext, opts, postcssOpts, warnings) {
17+
module.exports = function(t, file, opts, postcssOpts, warnings) {
1818
opts = Object.assign({ path: "test/fixtures/imports" }, opts)
19+
if (typeof file === "string") file = { name: file, ext: ".css" }
20+
const { name, ext } = file
21+
1922
return postcss(atImport(opts))
2023
.process(read(name, ext), postcssOpts || {})
2124
.then(result => {
2225
const actual = result.css
2326
const expected = read(`${name}.expected`)
2427
// handy thing: checkout actual in the *.actual.css file
25-
fs.writeFile(`test/fixtures/${name}.actual.css`, actual)
28+
fs.writeFile(`test/fixtures/${name}.actual.css`, actual, err => {
29+
if (err) console.warn(`Warning: ${err}; not fatal, continuing`)
30+
})
2631
t.is(actual, expected)
2732
if (!warnings) warnings = []
2833
result.warnings().forEach((warning, index) => {

test/helpers/compare-fixtures.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

test/import.js

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,22 @@ import test from "ava"
77
import postcss from "postcss"
88

99
// internal tooling
10-
import compareFixtures from "./helpers/compare-fixtures"
10+
import checkFixture from "./helpers/check-fixture"
1111

1212
// plugin
1313
import atImport from ".."
1414

15-
test("should import stylsheets", t => {
16-
return compareFixtures(t, "simple")
17-
})
15+
test("should import stylsheets", checkFixture, "simple")
1816

19-
test("should not import a stylsheet twice", t => {
20-
return compareFixtures(t, "no-duplicate")
21-
})
17+
test("should not import a stylsheet twice", checkFixture, "no-duplicate")
2218

23-
test("should be able to import a stylsheet twice", t => {
24-
return compareFixtures(t, "duplicates", { skipDuplicates: false })
19+
test("should be able to import a stylsheet twice", checkFixture, "duplicates", {
20+
skipDuplicates: false,
2521
})
2622

27-
test("should import stylsheets with same content", t => {
28-
return compareFixtures(t, "same")
29-
})
23+
test("should import stylsheets with same content", checkFixture, "same")
3024

31-
test("should ignore & adjust external import", t => {
32-
return compareFixtures(t, "ignore")
33-
})
25+
test("should ignore & adjust external import", checkFixture, "ignore")
3426

3527
test("should not fail with only one absolute import", t => {
3628
const base = "@import url(http://)"
@@ -83,15 +75,14 @@ test("inlined @import should keep PostCSS AST references clean", t => {
8375
})
8476
})
8577

86-
test("should work with empty files", t => {
87-
return compareFixtures(
88-
t,
89-
"empty-and-useless",
90-
{ path: "test/fixtures/imports" },
91-
null,
92-
[`${path.resolve("test/fixtures/imports/empty.css")} is empty`]
93-
)
94-
})
78+
test(
79+
"should work with empty files",
80+
checkFixture,
81+
"empty-and-useless",
82+
{ path: "test/fixtures/imports" },
83+
null,
84+
[`${path.resolve("test/fixtures/imports/empty.css")} is empty`]
85+
)
9586

9687
test("should work with no styles without throwing an error", t => {
9788
return postcss().use(atImport()).process("").then(result => {

test/media.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
import test from "ava"
33

44
// internal tooling
5-
import compareFixtures from "./helpers/compare-fixtures"
5+
import checkFixture from "./helpers/check-fixture"
66

7-
test("should resolve media queries of import statements", t => {
8-
return compareFixtures(t, "media-import")
9-
})
7+
test(
8+
"should resolve media queries of import statements",
9+
checkFixture,
10+
"media-import"
11+
)
1012

11-
test("should resolve media queries", t => {
12-
return compareFixtures(t, "media-query")
13-
})
13+
test("should resolve media queries", checkFixture, "media-query")
1414

15-
test("should resolve content inside import with media queries", t => {
16-
return compareFixtures(t, "media-content")
17-
})
15+
test(
16+
"should resolve content inside import with media queries",
17+
checkFixture,
18+
"media-content"
19+
)
1820

19-
test("should join correctly media queries", t => {
20-
return compareFixtures(t, "media-join")
21-
})
21+
test("should join correctly media queries", checkFixture, "media-join")

test/order.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import test from "ava"
33

44
// internal tooling
5-
import compareFixtures from "./helpers/compare-fixtures"
5+
import checkFixture from "./helpers/check-fixture"
66

77
test(`should order nested imports correctly`, t => {
88
let first = true
99
const path = require("path")
1010

11-
return compareFixtures(t, "order", {
11+
return checkFixture(t, "order", {
1212
path: "test/fixtures/imports",
1313
resolve: id => {
1414
return new Promise(res => {

test/plugins.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import test from "ava"
33
import postcss from "postcss"
44

55
// internal tooling
6-
import compareFixtures from "./helpers/compare-fixtures"
6+
import checkFixture from "./helpers/check-fixture"
77

88
// plugin
99
import atImport from ".."
1010

1111
test("should apply plugins to root", t => {
1212
const atRules = []
1313
const rules = []
14-
return compareFixtures(t, "plugins", {
14+
return checkFixture(t, "plugins", {
1515
plugins: [
1616
css => {
1717
css.walk(node => {

0 commit comments

Comments
 (0)