Skip to content

Commit

Permalink
Merge branch 'release/1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
davesag committed Nov 16, 2019
2 parents e474742 + 412c429 commit e24ac9b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 5 deletions.
73 changes: 70 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ npm i -D sequelize-test-helpers

## Examples

### Unit Testing Models
### Unit testing models created with `sequelize.define`

**Note**: See below for how to test models created using `Model.init`

Let's say you have a Sequelize model `User` as follows:

Expand Down Expand Up @@ -138,7 +140,7 @@ describe('src/models/User', () => {
})
```

### Built in checks
### Built-in checks

| Check | What it does |
| --- | --- |
Expand Down Expand Up @@ -289,9 +291,74 @@ describe('src/utils/save', () => {

As a convenience, `makeMockModels` will automatically populate your `mockModels` with mocks of all of the models defined in your `src/models` folder (or if you have a `.sequelizerc` file it will look for the `model-path` in that). Simply override any of the specific models you need to do stuff with.

### Testing models created with `Model.init`

Sequelize also allows you to create models by extending `Sequelize.Model` and invoking its static `init` function as follows:

**Note**: creating your models this way makes it harder to test their use.

```js
const { Model, DataTypes } = require('sequelize')

const factory = sequelize => {
class User extends Model {}
User.init(
{
firstName: DataTypes.STRING,
lastName: DataTypes.STRING
},
{ sequelize, modelName: 'User' }
)
return User
}

module.exports = factory
```

You can test this using `sequelize-test-helpers`, `sinon`, and `proxyquire`.

```js
const { expect } = require('chai')
const { spy } = require('sinon')
const proxyquire = require('proxyquire')
const { sequelize, Sequelize } = require('sequelize-test-helpers')

describe('src/models/User', () => {
const { DataTypes } = Sequelize

const UserFactory = proxyquire('src/models/User', {
sequelize: Sequelize
})

let User

before(() => {
User = UserFactory(sequelize)
})

// It's important you do this
after(() => {
User.init.resetHistory()
})

it('called User.init with the correct parameters', () => {
expect(User.init).to.have.been.calledWith(
{
firstName: DataTypes.STRING,
lastName: DataTypes.STRING
},
{
sequelize,
modelName: 'User'
}
)
})
})
```

### Listing your models

It's useful to be able to generate a list of the names of your models.
Assuming your `src/models/index.js` (or your equivalent) exports all your models, it's useful to be able to generate a list of their names.

```js
const { listModels } = require('sequelize-test-helpers')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sequelize-test-helpers",
"version": "1.1.3",
"version": "1.2.0",
"description": "A collection of utilities to help with unit-testing sequelize models",
"author": "Dave Sag <[email protected]>",
"type": "commonjs",
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const checkUniqueIndex = require('./checks/checkUniqueIndex')
const dataTypes = require('./dataTypes')
const sequelize = require('./sequelize')
const { makeMockModels, listModels } = require('./mockModels')
const Sequelize = require('./mockSequelize')

module.exports = {
checkHookDefined,
Expand All @@ -18,5 +19,6 @@ module.exports = {
dataTypes,
listModels,
makeMockModels,
sequelize
sequelize,
Sequelize
}
8 changes: 8 additions & 0 deletions src/mockSequelize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { spy } = require('sinon')

const DataTypes = require('./dataTypes')

class Model {}
Model.init = spy()

module.exports = { Model, DataTypes }
23 changes: 23 additions & 0 deletions test/unit/mockSequelize.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { expect } = require('chai')

const Sequelize = require('../../src/mockSequelize')
const DataTypes = require('../../src/dataTypes')

describe('src/mockSequelize', () => {
it('has Model', () => {
expect(Sequelize).to.have.property('Model')
})

it('Model is a class', () => {
expect(Sequelize.Model).to.be.a('function')
expect(Sequelize.Model.constructor).to.be.a('function')
})

it('Model has a static init function', () => {
expect(Sequelize.Model.init).to.be.a('function')
})

it('has DataTypes', () => {
expect(Sequelize).to.have.property('DataTypes', DataTypes)
})
})

0 comments on commit e24ac9b

Please sign in to comment.