Skip to content

Commit

Permalink
Merge branch 'master' into tech/updateLintingSetup
Browse files Browse the repository at this point in the history
* master:
  3.0.0
  chore(ci): drop Node.js 6 from the test matrix
  fix: support latest knex version (v0.20.13)
  chore: update knex to v0.20.13
  chore: update mocha to v7
  chore: run tests on Node.js 12 locally
  chore: switch dev environment to Node.js 12
  test: do not assert on `require` error messages
  chore: add a password for the postgres db
  chore: fix lint errors
  chore: remove unexpected-fs and use mock-fs directly
  chore(ci): add Node.js 6 to the test matrix
  chore: remove Travic CI builds
  chore(ci): refactor `npm ci` => `npm install`
  chore(nyc): enable text and lcov reports by default
  chore: add GitHub Actions file
  fix: display rejection reason for failed queries
  refactor: forward flags to expect.it
  chore: remove unnecessary COPY directive
  Update proxyquire to version 2.1.2
  • Loading branch information
papandreou committed Apr 11, 2020
2 parents 5d1b2be + b652099 commit c6e7193
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 87 deletions.
86 changes: 86 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,86 @@
name: CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
lint:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Use Node.js 12.x
uses: actions/setup-node@v1
with:
node-version: 12.x

- name: Use npm cache
uses: actions/cache@v1
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install dependencies
run: npm install

- name: Run lint
run: npm run lint

test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [8.x, 10.x, 12.x]

services:
postgres9:
image: postgres:9.6-alpine
env:
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Use npm cache
uses: actions/cache@v1
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install dependencies
run: npm install

- name: Run coverage
run: npm run coverage
env:
PGHOST: localhost
PGPORT: 5432

- name: Send coverage to coveralls
if: matrix.node-version == '12.x'
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .nvmrc
@@ -1 +1 @@
6.6.0
12
10 changes: 10 additions & 0 deletions .nycrc.json
@@ -0,0 +1,10 @@
{
"cache": true,
"include": [
"lib"
],
"reporter": [
"lcov",
"text"
]
}
13 changes: 0 additions & 13 deletions .travis.yml

This file was deleted.

5 changes: 1 addition & 4 deletions Dockerfile
@@ -1,6 +1,3 @@
# unexpected-fs does not support node >= 8
FROM node:6-alpine
FROM node:12-alpine

WORKDIR /usr/src/app

COPY . .
2 changes: 2 additions & 0 deletions docker-compose.yml
Expand Up @@ -3,6 +3,8 @@ services:
postgres:
image: postgres:9.6
container_name: unexpected-knex
environment:
POSTGRES_PASSWORD: postgres

unexpected-knex:
build: .
Expand Down
85 changes: 54 additions & 31 deletions lib/unexpected-knex.js
@@ -1,31 +1,33 @@
var path = require('path');
var util = require('util');
var Knex = require('knex');
var proxyquire = require('proxyquire');
var QueryBuilder = require('knex/lib/query/builder');
var knexMigrationListResolver = require('knex/lib/migrate/migration-list-resolver');

var unexpectedKnexMigratorFilename;
var getUnexpectedKnexMigratorFilenames = function(filenames) {
var filenameIndex = filenames.indexOf(unexpectedKnexMigratorFilename);
return filenames.slice(0, filenameIndex + 1);
var getUnexpectedKnexMigratorFilenames = function(sources) {
var filenameIndex = sources.findIndex(function (source) {
return source.file === unexpectedKnexMigratorFilename;
});
return sources.slice(0, filenameIndex + 1);
};
var unexpectedKnexMigrationListResolver = Object.assign(
{},
knexMigrationListResolver,
{
listAllAndCompleted(config, trxOrKnex, absoluteConfigDir) {
listAllAndCompleted(config, trxOrKnex) {
return knexMigrationListResolver
.listAllAndCompleted(config, trxOrKnex, absoluteConfigDir)
.then(function([filenames, completed]) {
return [getUnexpectedKnexMigratorFilenames(filenames), completed];
.listAllAndCompleted(config, trxOrKnex)
.then(function([sources, completed]) {
return [getUnexpectedKnexMigratorFilenames(sources), completed];
});
}
}
);

var UnexpectedKnexMigrator = proxyquire('knex/lib/migrate', {
var UnexpectedKnexMigrator = proxyquire('knex/lib/migrate/Migrator', {
'./migration-list-resolver': unexpectedKnexMigrationListResolver
});
}).Migrator;

function MigrationTest(knex, expect) {
this.knex = knex;
Expand Down Expand Up @@ -218,6 +220,7 @@ function init(expect) {
'<knexQuery> to have rows [exhaustively] satisfying <expect.it>'
],
function(expect, query, value) {
expect.errorMode = 'defaultOrNested';
return expect(
query.select(),
'to be fulfilled with value [exhaustively] satisfying',
Expand All @@ -227,16 +230,19 @@ function init(expect) {
);

expect.addAssertion('<knexQuery> [not] to be empty', function(expect, query) {
return query.select().then(function(rows) {
expect(rows, '[not] to be empty');
});
expect.errorMode = 'defaultOrNested';
return expect(
query,
'to have rows satisfying',
expect.it('[not] to be empty')
);
});

expect.addAssertion(
'<knexQuery> to have a row [exhaustively] satisfying <object>',
function(expect, query, value) {
expect.errorMode = 'defaultOrNested';
if (Object.keys(value).length < 1) {
expect.errorMode = 'nested';
return expect.fail('cannot assert that a row has no columns or fields');
}
return expect(
Expand All @@ -254,14 +260,14 @@ function init(expect) {
expect.addAssertion(
'<knexQuery> to have sorted rows [exhaustively] satisfying <array>',
function(expect, subject, value) {
const exhaustive = expect.flags.exhaustively;
expect.errorMode = 'defaultOrNested';
return expect(
subject,
'to have rows satisfying',
expect.it(
'sorted by',
ascendingOrder,
exhaustive ? 'to exhaustively satisfy' : 'to satisfy',
'to [exhaustively] satisfy',
value
)
);
Expand All @@ -271,21 +277,21 @@ function init(expect) {
expect.addAssertion(
'<Promise> to be fulfilled with sorted rows [exhaustively] satisfying <array>',
function(expect, subject, value) {
const exhaustive = expect.flags.exhaustively;
expect.errorMode = 'defaultOrNested';
return expect(
subject,
'to be fulfilled with value satisfying',
expect.it(
'sorted by',
ascendingOrder,
exhaustive ? 'to exhaustively satisfy' : 'to satisfy',
'to [exhaustively] satisfy',
value
)
);
}
);

expect.addAssertion('<knex> to apply migration <string>', function(
expect.addAssertion('<knex> to apply migration <string>', async function(
expect,
knex,
filename
Expand All @@ -309,18 +315,23 @@ function init(expect) {
}

unexpectedKnexMigratorFilename = filename;

var migrator = new UnexpectedKnexMigrator(knex);
var test = new MigrationTest(knex, expect);
var migration;
try {
var directory = migrator._absoluteConfigDir();
migration = require(path.resolve(directory, filename));
} catch (e) {
return fail('cannot load migration', e);
}

var test = new MigrationTest(knex, expect);
if (typeof migration.test === 'function') {
migration.test.call(test);
function getMigration() {
return migrator.config.migrationSource
.getMigrations()
.then(function(sources) {
const source = sources.find(function(migration) {
return migration.file === filename;
});
if (!source) {
throw new Error('migration ' + util.inspect(filename) + ' not found');
}
return migrator.config.migrationSource.getMigration(source);
});
}

function migrateUp() {
Expand All @@ -331,10 +342,22 @@ function init(expect) {
return migrator.rollback();
}

return test
._runHook('beforeUp')
return getMigration()
.then(function(_migration) {
migration = _migration;
})
.catch(function(e) {
return fail('beforeUp failed with', e);
return fail('cannot load migration', e);
})
.then(function() {
if (typeof migration.test === 'function') {
migration.test.call(test);
}
})
.then(function() {
return test._runHook('beforeUp').catch(function(e) {
return fail('beforeUp failed with', e);
});
})
.then(function() {
return migrateUp().catch(function(e) {
Expand Down
17 changes: 7 additions & 10 deletions package.json
@@ -1,16 +1,14 @@
{
"name": "unexpected-knex",
"version": "2.0.0",
"version": "3.0.0",
"description": "Support for testing Knex.js models and migrations using Unexpected",
"main": "lib/unexpected-knex.js",
"scripts": {
"lint": "eslint . && prettier --check '**/*.js'",
"lint:fix": "eslint --fix . && prettier --write '**/*.js'",
"test:mocha": "mocha --exit",
"test": "docker-compose run --rm unexpected-knex",
"coverage": "ASSERT_ERROR_OUTPUT=false nyc mocha",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"ci": "npm-run-all lint coverage coveralls"
"coverage": "ASSERT_ERROR_OUTPUT=false nyc mocha"
},
"keywords": [
"test",
Expand All @@ -24,11 +22,10 @@
"author": "Joel Mukuthu <joelmukuthu@gmail.com>",
"license": "MIT",
"peerDependencies": {
"knex": "^0.15.2",
"knex": "^0.16.0",
"unexpected": "^10.27.0 || ^11.0.0"
},
"devDependencies": {
"coveralls": "^3.0.2",
"dedent-js": "^1.0.1",
"eslint": "^5.12.0",
"eslint-config-prettier": "^5.1.0",
Expand All @@ -39,14 +36,14 @@
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.0",
"knex": "^0.15.2",
"mocha": "^6.0.0",
"knex": "^0.20.13",
"mocha": "^7.1.1",
"mock-fs": "^4.11.0",
"npm-run-all": "^4.1.5",
"nyc": "^14.0.0",
"pg": "^7.7.1",
"prettier": "~1.18.2",
"unexpected": "^11.0.0",
"unexpected-fs": "^3.0.0",
"unexpected-require": "^2.1.0"
},
"nyc": {
Expand All @@ -67,6 +64,6 @@
},
"homepage": "https://github.com/unexpectedjs/unexpected-knex#readme",
"dependencies": {
"proxyquire": "2.1.0"
"proxyquire": "2.1.2"
}
}

0 comments on commit c6e7193

Please sign in to comment.