From 1e21a9559d9701193746f8b02c8f709b6e1be9b7 Mon Sep 17 00:00:00 2001 From: Ryan Zimmerman Date: Fri, 24 Sep 2021 08:16:26 -0400 Subject: [PATCH] Port tests to ESM --- test/base.js | 11 +++++------ test/cli.js | 9 ++++----- test/config.js | 11 +++++------ test/dir.js | 11 +++++------ test/error.js | 7 +++---- test/ext.js | 11 +++++------ test/fixtures/_bad-plugin.js | 1 - test/glob.js | 11 +++++------ test/helpers/clean.js | 3 +-- test/helpers/cli.js | 7 +++---- test/helpers/env.js | 13 ++++++------- test/helpers/read.js | 5 ++--- test/helpers/tmp.js | 7 +++---- test/map.js | 11 +++++------ test/misc.js | 5 ++--- test/parser.js | 9 ++++----- test/replace.js | 13 ++++++------- test/stdin.js | 13 ++++++------- test/stdout.js | 11 +++++------ test/stringifier.js | 9 ++++----- test/syntax.js | 9 ++++----- test/use.js | 9 ++++----- test/watch.js | 23 +++++++++++------------ 23 files changed, 98 insertions(+), 121 deletions(-) diff --git a/test/base.js b/test/base.js index 32eff27..496ca14 100644 --- a/test/base.js +++ b/test/base.js @@ -1,10 +1,9 @@ -'use strict' -const test = require('ava') -const path = require('path') +import test from 'ava' +import path from 'path' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('--base --dir works', async (t) => { const dir = tmp() diff --git a/test/cli.js b/test/cli.js index 0cdc7dc..3adab9b 100644 --- a/test/cli.js +++ b/test/cli.js @@ -1,9 +1,8 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('works with defaults', async (t) => { const output = tmp('output.css') diff --git a/test/config.js b/test/config.js index b8b3de5..a4eb8c6 100644 --- a/test/config.js +++ b/test/config.js @@ -1,11 +1,10 @@ -'use strict' -const test = require('ava') -const path = require('path') +import test from 'ava' +import path from 'path' -const ENV = require('./helpers/env.js') +import ENV from './helpers/env.js' -const cli = require('./helpers/cli.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import read from './helpers/read.js' test('supports common config', async (t) => { const env = `module.exports = { diff --git a/test/dir.js b/test/dir.js index ec4e60e..695f198 100644 --- a/test/dir.js +++ b/test/dir.js @@ -1,10 +1,9 @@ -'use strict' -const test = require('ava') -const path = require('path') +import test from 'ava' +import path from 'path' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('--dir works', async (t) => { const dir = tmp() diff --git a/test/error.js b/test/error.js index 4309023..9901f47 100644 --- a/test/error.js +++ b/test/error.js @@ -1,8 +1,7 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const tmp = require('./helpers/tmp.js') -const cli = require('./helpers/cli.js') +import tmp from './helpers/tmp.js' +import cli from './helpers/cli.js' test('multiple input files && --output', (t) => { return cli(['test/fixtures/*.css', '-o', tmp()]).then(({ error, code }) => { diff --git a/test/ext.js b/test/ext.js index c646630..329f2c4 100644 --- a/test/ext.js +++ b/test/ext.js @@ -1,11 +1,10 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const fs = require('fs-extra') -const path = require('path') +import fs from 'fs-extra' +import path from 'path' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' test('--ext works', async (t) => { const dir = tmp() diff --git a/test/fixtures/_bad-plugin.js b/test/fixtures/_bad-plugin.js index 7a4ffeb..2911e95 100644 --- a/test/fixtures/_bad-plugin.js +++ b/test/fixtures/_bad-plugin.js @@ -1,2 +1 @@ -'use strict' throw new Error('This fails') diff --git a/test/glob.js b/test/glob.js index e60b75a..33917ad 100644 --- a/test/glob.js +++ b/test/glob.js @@ -1,10 +1,9 @@ -'use strict' -const test = require('ava') -const path = require('path') +import test from 'ava' +import path from 'path' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('works with glob patterns', async (t) => { const output = tmp() diff --git a/test/helpers/clean.js b/test/helpers/clean.js index cabfc72..bf0ab9e 100644 --- a/test/helpers/clean.js +++ b/test/helpers/clean.js @@ -1,5 +1,4 @@ -'use strict' -const fs = require('fs-extra') +import fs from 'fs-extra' Promise.all([ fs.emptyDir('./test/fixtures/.tmp/'), diff --git a/test/helpers/cli.js b/test/helpers/cli.js index c189e00..565d3ff 100644 --- a/test/helpers/cli.js +++ b/test/helpers/cli.js @@ -1,8 +1,7 @@ -'use strict' -const path = require('path') -const { exec } = require('child_process') +import path from 'path' +import { exec } from 'child_process' -module.exports = function (args, cwd) { +export default function (args, cwd) { return new Promise((resolve) => { exec( `node ${path.resolve('index.js')} ${args.join(' ')}`, diff --git a/test/helpers/env.js b/test/helpers/env.js index b389370..e6f48c2 100644 --- a/test/helpers/env.js +++ b/test/helpers/env.js @@ -1,11 +1,10 @@ -'use strict' -const fs = require('fs-extra') -const path = require('path') -const globby = require('globby') +import fs from 'fs-extra' +import path from 'path' +import globby from 'globby' -const tmp = require('./tmp.js') +import tmp from './tmp.js' -module.exports = function (config, fixtures) { +export default function (config, fixtures) { fixtures = fixtures || '**/*' const dir = tmp() @@ -15,6 +14,6 @@ module.exports = function (config, fixtures) { return fs.copy(path.join('test/fixtures', item), path.join(dir, item)) }) }), - fs.outputFile(path.join(dir, 'postcss.config.js'), config), + fs.outputFile(path.join(dir, 'postcss.config.cjs'), config), ]).then(() => dir) } diff --git a/test/helpers/read.js b/test/helpers/read.js index 88b6421..e6572eb 100644 --- a/test/helpers/read.js +++ b/test/helpers/read.js @@ -1,7 +1,6 @@ -'use strict' -const { readFile } = require('fs-extra') +import { readFile } from 'fs/promises' -module.exports = function (path) { +export default function (path) { return readFile(path, 'utf8').then( (content) => content.replace(/\r\n/g, '\n') // normalize line endings on Windows ) diff --git a/test/helpers/tmp.js b/test/helpers/tmp.js index a23fa35..bfa1369 100644 --- a/test/helpers/tmp.js +++ b/test/helpers/tmp.js @@ -1,8 +1,7 @@ -'use strict' -const path = require('path') -const { v4: uuid } = require('uuid') +import path from 'path' +import { v4 as uuid } from 'uuid' -module.exports = function (ext) { +export default function (ext) { ext = ext || '' return path.join('test/fixtures/.tmp', uuid(), ext) diff --git a/test/map.js b/test/map.js index 8e98b61..4783010 100644 --- a/test/map.js +++ b/test/map.js @@ -1,10 +1,9 @@ -'use strict' -const test = require('ava') -const fs = require('fs-extra') +import test from 'ava' +import fs from 'fs-extra' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('inline maps are generated by default', async (t) => { const output = tmp('output.css') diff --git a/test/misc.js b/test/misc.js index 9240e32..812b6d0 100644 --- a/test/misc.js +++ b/test/misc.js @@ -1,7 +1,6 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const cli = require('./helpers/cli.js') +import cli from './helpers/cli.js' test('--help', async (t) => { const help = await cli(['--help']) diff --git a/test/parser.js b/test/parser.js index 1ac9805..689d9b7 100644 --- a/test/parser.js +++ b/test/parser.js @@ -1,9 +1,8 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('--parser works', async (t) => { const output = tmp('output.css') diff --git a/test/replace.js b/test/replace.js index f2712a2..dd24b98 100644 --- a/test/replace.js +++ b/test/replace.js @@ -1,12 +1,11 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const fs = require('fs-extra') -const path = require('path') +import fs from 'fs-extra' +import path from 'path' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('--replace works', async (t) => { const dir = tmp() diff --git a/test/stdin.js b/test/stdin.js index 962e678..9fb0679 100644 --- a/test/stdin.js +++ b/test/stdin.js @@ -1,12 +1,11 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const fs = require('fs-extra') -const path = require('path') -const { exec } = require('child_process') +import fs from 'fs-extra' +import path from 'path' +import { exec } from 'child_process' -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test.cb('reads from stdin', (t) => { const output = tmp('output.css') diff --git a/test/stdout.js b/test/stdout.js index 1a14270..68b6b4b 100644 --- a/test/stdout.js +++ b/test/stdout.js @@ -1,11 +1,10 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const fs = require('fs-extra') -const path = require('path') -const { exec } = require('child_process') +import fs from 'fs-extra' +import path from 'path' +import { exec } from 'child_process' -const read = require('./helpers/read.js') +import read from './helpers/read.js' test.cb('writes to stdout', (t) => { const cp = exec( diff --git a/test/stringifier.js b/test/stringifier.js index db3ef28..69adaff 100644 --- a/test/stringifier.js +++ b/test/stringifier.js @@ -1,9 +1,8 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('--stringifier works', async (t) => { const output = tmp('output.sss') diff --git a/test/syntax.js b/test/syntax.js index c4295f9..b17e1fd 100644 --- a/test/syntax.js +++ b/test/syntax.js @@ -1,9 +1,8 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('--syntax works', async (t) => { const output = tmp('output.sss') diff --git a/test/use.js b/test/use.js index 25400f6..c35d6cd 100644 --- a/test/use.js +++ b/test/use.js @@ -1,9 +1,8 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('--use works', async (t) => { const output = tmp('i.css') diff --git a/test/watch.js b/test/watch.js index 8c490eb..b38b81d 100644 --- a/test/watch.js +++ b/test/watch.js @@ -1,14 +1,13 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const fs = require('fs-extra') -const path = require('path') -const { exec, spawn } = require('child_process') -const chokidar = require('chokidar') +import fs from 'fs-extra' +import path from 'path' +import { exec, spawn } from 'child_process' +import chokidar from 'chokidar' -const ENV = require('./helpers/env.js') -const read = require('./helpers/read.js') -const tmp = require('./helpers/tmp.js') +import ENV from './helpers/env.js' +import read from './helpers/read.js' +import tmp from './helpers/tmp.js' // XXX: All the tests in this file are skipped on the CI; too flacky there const testCb = process.env.CI ? test.cb.skip : test.cb @@ -224,7 +223,7 @@ testCb('--watch watches dependencies', (t) => { ENV('', ['s.css', 'a.css', 'b.css']).then((dir) => { fs.writeFile( - path.join(dir, 'postcss.config.js'), + path.join(dir, 'postcss.config.cjs'), ` const fs = require('fs') module.exports = { @@ -318,7 +317,7 @@ testCb('--watch watches directory dependencies', (t) => { ENV('', ['s.css', 'base/level-1/b.css', 'base/level-1/level-2/a.css']).then( (dir) => { fs.writeFile( - path.join(dir, 'postcss.config.js'), + path.join(dir, 'postcss.config.cjs'), ` const fs = require('fs') module.exports = { @@ -427,7 +426,7 @@ testCb( 'base/level-1/level-2/unrelated.md', ]).then((dir) => { fs.writeFile( - path.join(dir, 'postcss.config.js'), + path.join(dir, 'postcss.config.cjs'), ` const fs = require('fs') module.exports = {