Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3751: Esm interop flag #3616

Merged
merged 1 commit into from Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion bin/cli.js
Expand Up @@ -30,6 +30,10 @@ const fsPromised = {
};

function initKnex(env, opts) {
if (opts.esm) {
// enable esm interop via 'esm' module
require = require('esm')(module);
}
checkLocalModule(env);
if (process.cwd() !== env.cwd) {
process.chdir(env.cwd);
Expand Down Expand Up @@ -110,7 +114,8 @@ function invoke(env) {
.option(
'--env [name]',
'environment, default: process.env.NODE_ENV || development'
);
)
.option('--esm', 'Enable ESM interop.');

commander
.command('init')
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -32,6 +32,7 @@
"colorette": "1.1.0",
"commander": "^4.0.1",
"debug": "4.1.1",
"esm": "^3.2.25",
"getopts": "2.2.5",
"inherits": "~2.0.4",
"interpret": "^2.0.0",
Expand Down
16 changes: 16 additions & 0 deletions test/jake-util/knexfile-esm/.eslintrc.json
@@ -0,0 +1,16 @@
{
"extends": "../../../.eslintrc.js",
"env": {
"browser": true,
"es6": true
},
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {}
}
14 changes: 14 additions & 0 deletions test/jake-util/knexfile-esm/knexfile.js
@@ -0,0 +1,14 @@
const config = {
client: 'sqlite3',
connection: {
filename: '../test.sqlite3',
},
migrations: {
directory: './migrations',
},
seeds: {
directory: './seeds',
},
};
/** Named exports */
export const { client, connection, migrations, seeds } = config;
2 changes: 2 additions & 0 deletions test/jake-util/knexfile-esm/migrations/one.js
@@ -0,0 +1,2 @@
export function up(...args) {}
export function down(...args) {}
1 change: 1 addition & 0 deletions test/jake-util/knexfile-esm/seeds/one.js
@@ -0,0 +1 @@
export function seed(...args) {}
14 changes: 14 additions & 0 deletions test/jake/jakelib/migrate-test.js
Expand Up @@ -734,6 +734,20 @@ test('migrate:list prints migrations both completed and pending', async (temp) =
);
});

test('migrate runs "esm" modules', (temp) => {
const db = knexfile.connection.filename;
if (fs.existsSync(db)) {
fs.unlinkSync(db);
}

return assertExec(
`node ${KNEX} --esm migrate:latest --knexfile=test/jake-util/knexfile-esm/knexfile.js --knexpath=../knex.js`
).then(({ stdout }) => {
assert.include(stdout, 'Batch 1 run: 1 migrations');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cli-testlab 1.10.0 supports multiple assertions, so if you bump dependency, you can do

return assertExec(
    `node ${KNEX} --esm migrate:latest --knexfile=test/jake-util/knexfile-esm/knexfile.js --knexpath=../knex.js`, {
      expectedOutput: ['Batch 1 run: 1 migrations', 'one.js']
    }
  )

assert.include(stdout, 'one.js');
});
});

module.exports = {
taskList,
};
10 changes: 10 additions & 0 deletions test/jake/jakelib/seed-test.js
Expand Up @@ -55,6 +55,16 @@ test(taskList, 'Handles seeding errors correctly', (temp) => {
});
});

test(taskList, 'seed:run runs "esm" files', () => {
return assertExec(
`node ${KNEX} --esm seed:run --knexfile=test/jake-util/knexfile-esm/knexfile.js`
).then(({ stdout }) => {
assert.include(stdout, 'Ran 1 seed files');
assert.notInclude(stdout, 'first.js');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use notExpectedOutput and expectedOutput params for assertExec to avoid then chaining

assert.notInclude(stdout, 'second.js');
});
});

module.exports = {
taskList,
};