diff --git a/packages/rollup/test/__snapshots__/rollup-watch.test.js.snap b/packages/rollup/test/__snapshots__/rollup-watch.test.js.snap new file mode 100644 index 000000000..a403562c4 --- /dev/null +++ b/packages/rollup/test/__snapshots__/rollup-watch.test.js.snap @@ -0,0 +1,68 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`/rollup.js watch mode should correctly add new css files 1`] = ` +"/* packages/rollup/test/output/watch/new-file/one.css */ +.mc54f12712_one { + color: red; +}" +`; + +exports[`/rollup.js watch mode should correctly remove & re-add dependencies 1`] = ` +"/* packages/rollup/test/output/watch/watch-changed/two.css */ +.mcd26cbf72_two { + color: red; +} +/* packages/rollup/test/output/watch/watch-changed/one.css */ +.mc8f47b33e_one { + color: red; +}" +`; + +exports[`/rollup.js watch mode should correctly remove & re-add dependencies 2`] = ` +"/* packages/rollup/test/output/watch/watch-changed/two.css */ +.mcd26cbf72_two { + color: blue; +} +/* packages/rollup/test/output/watch/watch-changed/one.css */ +.mc8f47b33e_one { + color: red; +}" +`; + +exports[`/rollup.js watch mode should correctly update files within the dependency graph 1`] = ` +"/* packages/rollup/test/output/watch/dep-graph/one.css */ +.mca3136a1b_one { + color: red; +} +/* packages/rollup/test/output/watch/dep-graph/two.css */ +.mc6f857f1c_two { + + color: blue; +}" +`; + +exports[`/rollup.js watch mode should correctly update files within the dependency graph 2`] = ` +"/* packages/rollup/test/output/watch/dep-graph/one.css */ +.mca3136a1b_one { + color: green; +} +/* packages/rollup/test/output/watch/dep-graph/two.css */ +.mc6f857f1c_two { + + color: blue; +}" +`; + +exports[`/rollup.js watch mode should generate output 1`] = ` +"/* packages/rollup/test/output/watch/change/watched.css */ +.mc5f9237d4_one { + color: red; +}" +`; + +exports[`/rollup.js watch mode should generate output 2`] = ` +"/* packages/rollup/test/output/watch/change/watched.css */ +.mc5f9237d4_two { + color: blue; +}" +`; diff --git a/packages/rollup/test/__snapshots__/rollup.test.js.snap b/packages/rollup/test/__snapshots__/rollup.test.js.snap index a65d9416e..145dbec85 100644 --- a/packages/rollup/test/__snapshots__/rollup.test.js.snap +++ b/packages/rollup/test/__snapshots__/rollup.test.js.snap @@ -285,52 +285,3 @@ exports[`/rollup.js shouldn't over-remove files from an existing processor insta } " `; - -exports[`/rollup.js watch should correctly add new css files in watch mode when files change 1`] = ` -"/* packages/rollup/test/output/one.css */ -.mc19ef5610_one { - color: red; -}" -`; - -exports[`/rollup.js watch should correctly update files within the dependency graph in watch mode when files change 1`] = ` -"/* packages/rollup/test/output/one.css */ -.mc19ef5610_one { - color: red; -} -/* packages/rollup/test/output/two.css */ -.mc32dbcb4b_two { - - color: blue; -}" -`; - -exports[`/rollup.js watch should correctly update files within the dependency graph in watch mode when files change 2`] = ` -"/* packages/rollup/test/output/one.css */ -.mc19ef5610_one { - color: green; -} -/* packages/rollup/test/output/two.css */ -.mc32dbcb4b_two { - - color: blue; -}" -`; - -exports[`/rollup.js watch should generate correct builds in watch mode when files change 1`] = ` -"/* packages/rollup/test/output/watched.css */ -.mc580619a9_one { color: red; } -/* packages/rollup/test/specimens/simple.css */ -.mc12d71625_fooga { - color: red; -}" -`; - -exports[`/rollup.js watch should generate correct builds in watch mode when files change 2`] = ` -"/* packages/rollup/test/output/watched.css */ -.mc580619a9_two { color: blue; } -/* packages/rollup/test/specimens/simple.css */ -.mc12d71625_fooga { - color: red; -}" -`; diff --git a/packages/rollup/test/rollup-watch.test.js b/packages/rollup/test/rollup-watch.test.js new file mode 100644 index 000000000..2c74aab91 --- /dev/null +++ b/packages/rollup/test/rollup-watch.test.js @@ -0,0 +1,245 @@ +/* eslint consistent-return: off */ +"use strict"; + +const fs = require("fs"); +const path = require("path"); + +const dedent = require("dedent"); +const shell = require("shelljs"); + +const output = path.resolve(__dirname, "./output/watch"); + +const read = require("test-utils/read.js")(output); +const write = require("test-utils/write.js")(output); +const exists = require("test-utils/exists.js")(__dirname); +const watching = require("test-utils/rollup-watching.js"); + +const plugin = require("../rollup.js"); + +const assetFileNames = "assets/[name][extname]"; +const format = "es"; +const map = false; + +describe("/rollup.js", () => { + describe("watch mode", () => { + const { watch } = require("rollup"); + let watcher; + + beforeAll(() => shell.rm("-rf", `${output}/*`)); + afterEach(() => watcher.close()); + + it("should generate output", (done) => { + // Create v1 of the files + write(`/change/watched.css`, dedent(` + .one { + color: red; + } + `)); + + write(`/change/watched.js`, dedent(` + import css from "./watched.css"; + console.log(css); + `)); + + // Start watching + watcher = watch({ + input : `${output}/change/watched.js`, + output : { + file : `${output}/change/watch-output.js`, + format, + assetFileNames, + }, + plugins : [ + plugin({ + map, + }), + ], + }); + + // Create v2 of the file after a bit + setTimeout(() => { + write(`./change/watched.css`, dedent(` + .two { + color: blue; + } + `)); + }, 200); + + watcher.on("event", watching((builds) => { + if(builds === 1) { + expect(read("./change/assets/watch-output.css")).toMatchSnapshot(); + + // continue watching + return; + } + + expect(read("./change/assets/watch-output.css")).toMatchSnapshot(); + + return done(); + })); + }); + + it("should correctly update files within the dependency graph", (done) => { + // Create v1 of the files + write(`./dep-graph/one.css`, dedent(` + .one { + color: red; + } + `)); + + write(`./dep-graph/two.css`, dedent(` + .two { + composes: one from "./one.css"; + + color: blue; + } + `)); + + write(`./dep-graph/watch.js`, dedent(` + import css from "./two.css"; + console.log(css); + `)); + + // Start watching + watcher = watch({ + input : require.resolve(path.join(output, "./dep-graph/watch.js")), + output : { + file : `${output}/dep-graph/watch-output.js`, + format, + assetFileNames, + }, + plugins : [ + plugin({ + map, + }), + ], + }); + + // Create v2 of the file after a bit + setTimeout(() => { + write(`./dep-graph/one.css`, dedent(` + .one { + color: green; + } + `)); + }, 200); + + watcher.on("event", watching((builds) => { + if(builds === 1) { + expect(read("dep-graph/assets/watch-output.css")).toMatchSnapshot(); + + // continue watching + return; + } + + expect(read("dep-graph/assets/watch-output.css")).toMatchSnapshot(); + + return done(); + })); + }); + + it("should correctly add new css files", (done) => { + // Create v1 of the files + write(`./new-file/one.css`, dedent(` + .one { + color: red; + } + `)); + + write(`./new-file/watch.js`, dedent(` + console.log("hello"); + `)); + + // Start watching + watcher = watch({ + input : require.resolve(path.join(output, "./new-file/watch.js")), + output : { + file : `${output}/new-file/watch-output.js`, + format, + assetFileNames, + }, + plugins : [ + plugin({ + map, + }), + ], + }); + + // Create v2 of the file after a bit + setTimeout(() => write(`./new-file/watch.js`, dedent(` + import css from "./one.css"; + + console.log(css); + `)), 200); + + watcher.on("event", watching((builds) => { + if(builds === 1) { + expect(exists("./new-file/assets/watch-output.css")).toBe(false); + + // continue watching + return; + } + + expect(read("./new-file/assets/watch-output.css")).toMatchSnapshot(); + + return done(); + })); + }); + + it("should correctly remove & re-add dependencies", (done) => { + // Create v1 of the files + write(`./watch-changed/one.css`, dedent(` + .one { + composes: two from "./two.css"; + color: red; + } + `)); + + write(`./watch-changed/two.css`, dedent(` + .two { + color: red; + } + `)); + + write(`./watch-changed/watch.js`, dedent(` + import css from "./one.css"; + console.log("hello"); + `)); + + // Start watching + watcher = watch({ + input : require.resolve(path.join(output, "./watch-changed/watch.js")), + output : { + file : `${output}/watch-changed/watch-output.js`, + format, + assetFileNames, + }, + plugins : [ + plugin({ + map, + }), + ], + }); + + // Create v2 of the file after a bit + setTimeout(() => write(`./watch-changed/two.css`, dedent(` + .two { + color: blue; + } + `)), 200); + + watcher.on("event", watching((builds) => { + if(builds === 1) { + expect(read("./watch-changed/assets/watch-output.css")).toMatchSnapshot(); + + // continue watching + return; + } + + expect(read("./watch-changed/assets/watch-output.css")).toMatchSnapshot(); + + return done(); + })); + }); + }); +}); diff --git a/packages/rollup/test/rollup.test.js b/packages/rollup/test/rollup.test.js index 89dd0ee07..b2aaa2493 100644 --- a/packages/rollup/test/rollup.test.js +++ b/packages/rollup/test/rollup.test.js @@ -1,8 +1,6 @@ -/* eslint consistent-return: off */ +/* eslint max-statements: "off" */ "use strict"; -const fs = require("fs"); - const { rollup } = require("rollup"); const dedent = require("dedent"); @@ -11,7 +9,6 @@ const shell = require("shelljs"); const read = require("test-utils/read.js")(__dirname); const exists = require("test-utils/exists.js")(__dirname); const namer = require("test-utils/namer.js"); -const watching = require("test-utils/rollup-watching.js"); const Processor = require("modular-css-core"); @@ -30,8 +27,6 @@ const output = "./packages/rollup/test/output"; const sourcemap = false; describe("/rollup.js", () => { - /* eslint max-statements: "off" */ - beforeAll(() => shell.rm("-rf", `${output}/*`)); it("should be a function", () => @@ -415,169 +410,6 @@ describe("/rollup.js", () => { ); }); - describe("watch", () => { - const { watch } = require("rollup"); - let watcher; - - afterEach(() => watcher.close()); - - it("should generate correct builds in watch mode when files change", (done) => { - // Create v1 of the file - fs.writeFileSync( - `${output}/watched.css`, - ".one { color: red; }" - ); - - // Start watching - watcher = watch({ - input : require.resolve("./specimens/watch.js"), - output : { - file : `${output}/watch/watch-output.js`, - format, - assetFileNames, - }, - plugins : [ - plugin({ - map, - }), - ], - }); - - // Create v2 of the file after a bit - setTimeout(() => fs.writeFileSync( - `${output}/watched.css`, - ".two { color: blue; }" - ), 200); - - watcher.on("event", watching((builds) => { - if(builds === 1) { - expect(read("watch/assets/watch-output.css")).toMatchSnapshot(); - - // continue watching - return; - } - - expect(read("watch/assets/watch-output.css")).toMatchSnapshot(); - - return done(); - })); - }); - - it("should correctly update files within the dependency graph in watch mode when files change", (done) => { - // Create v1 of the files - fs.writeFileSync(`${output}/one.css`, dedent(` - .one { - color: red; - } - `)); - - fs.writeFileSync(`${output}/two.css`, dedent(` - .two { - composes: one from "./one.css"; - - color: blue; - } - `)); - - fs.writeFileSync(`${output}/watch.js`, dedent(` - import css from "./two.css"; - console.log(css); - `)); - - // Start watching - watcher = watch({ - input : require.resolve("./output/watch.js"), - output : { - file : `${output}/watch-deps/watch-output.js`, - format, - assetFileNames, - }, - plugins : [ - plugin({ - map, - }), - ], - }); - - // Create v2 of the file after a bit - setTimeout(() => fs.writeFileSync(`${output}/one.css`, dedent(` - .one { - color: green; - } - `)), 200); - - watcher.on("event", watching((builds) => { - if(builds === 1) { - expect(read("watch-deps/assets/watch-output.css")).toMatchSnapshot(); - - // continue watching - return; - } - - expect(read("watch-deps/assets/watch-output.css")).toMatchSnapshot(); - - return done(); - })); - }); - - it("should correctly add new css files in watch mode when files change", (done) => { - // Create v1 of the files - fs.writeFileSync( - `${output}/one.css`, - dedent(` - .one { - color: red; - } - `) - ); - - fs.writeFileSync( - `${output}/watch.js`, - dedent(` - console.log("hello"); - `) - ); - - // Start watching - watcher = watch({ - input : require.resolve("./output/watch.js"), - output : { - file : `${output}/watch-new/watch-output.js`, - format, - assetFileNames, - }, - plugins : [ - plugin({ - map, - }), - ], - }); - - // Create v2 of the file after a bit - setTimeout(() => fs.writeFileSync( - `${output}/watch.js`, - dedent(` - import css from "./one.css"; - - console.log(css); - `) - ), 200); - - watcher.on("event", watching((builds) => { - if(builds === 1) { - expect(exists("watch-new/assets/watch-output.css")).toBe(false); - - // continue watching - return; - } - - expect(read("watch-new/assets/watch-output.css")).toMatchSnapshot(); - - return done(); - })); - }); - }); - describe("code splitting", () => { const experimentalCodeSplitting = true; const chunkFileNames = "[name].js";