@connectedcars/test
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

node-test

Connected Cars JavaScript/TypeScript testing utilities

Install

npm install --save-dev @connectedcars/test

Use

MySQL integration test

Start a mysqld in a tmp folder and migrate from files in ./migrations:

// Start a new mysqld in a tmp folder
const mySqlServer = new MySQLServer()

// Migrate the started server with migrations from the package @connectedcars/data
const mySqlClient = new MySQLClient({ port: await mySqlServer.getListenPort() })
const migrate = new Migrate({
  mysqlClient: mySqlClient,
  // First try local cache else use the one in @connectedcars/data
  cachePaths:['./cache', './node_modules/@connectedcars/data/cache'],
  migrationsDir: './node_modules/@connectedcars/data/migrations',
})
let migrationResult = await migrate.migrate() // Run all migrations

// Connect to the database and run some queries
const pool = await mySqlClient.getConnectionPool('myDataBase')
const databases: string[] = await this.mySqlClient.queryArray<string>(basePool,
  `
      SELECT SCHEMA_NAME as \`name\`
      FROM information_schema.SCHEMATA;
  `
)
const tableColumns = await this.mySqlClient.query<{ name: string; column: string; extra: string }>(basePool,
  `
      SELECT TABLE_NAME as \`name\`, COLUMN_NAME as \`column\`, EXTRA as extra
      FROM INFORMATION_SCHEMA.COLUMNS
      WHERE TABLE_SCHEMA = DATABASE()
      ORDER BY \`name\`;
  `
)

// Creates a copy of myDataBase with a new name and return a pool with this is a the default database
let myPoolCopy = this.mySqlClient.createDatabaseCopy('myDataBase')

To greatly speed up initial start and migrations add an npm script that builds startup cache:

package.json:

 "scripts": {
    "build-cache": "cache-migrations",
 }
npm run build-cache

EnvStub

Stubs all environment variable keys passed into the constructor. This allows you to set the variables in a test case (for example process.env.LOG_LEVEL = 'ERROR') without those changes persisting between test cases.

describe('database/config', () => {
  let envStub: EnvStub
  beforeEach(() => {
    envStub = new EnvStub(['CORE_DB', 'LOG_LEVEL'])
  })
  afterEach(() => {
    envStub.restore()
  })
  test('some test', () => {
    // CORE_DB and LOG_LEVEL are always undefined until explicitly defined in a test case
  })
})

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.2.2-30close-connections
2.0.173latest
2.0.1-12next
1.2.4-01test-postfix

Version History

VersionDownloads (Last 7 Days)Published
2.0.173
2.0.1-12
2.0.1-02
2.0.092
1.7.4-10
1.7.3-30
1.7.3-21
1.7.3-00
1.7.354
1.7.25
1.7.11
1.7.01
1.6.01
1.5.01
1.4.31
1.4.21
1.4.132
1.4.02
1.2.4-01
1.3.111
1.3.00
1.2.31
1.2.20
1.2.2-30
1.2.2-21
1.2.2-11
1.2.10
1.2.00
1.1.01
1.0.100
1.0.91
1.0.80
1.0.70
1.0.60
1.0.556
1.0.40
1.0.31
1.0.20
1.0.10
1.0.00
0.22.90
0.22.80
0.22.71
0.22.60
0.22.50
0.22.40
0.22.30
0.22.21
0.22.10
0.22.01
0.21.10
0.21.00
0.20.60
0.20.50
0.20.40
0.20.30
0.20.21
0.20.11
0.20.00
0.19.04
0.18.20
0.18.11
0.18.00
0.17.11
0.17.01
0.16.20
0.16.10
0.16.01
0.15.07
0.14.00
0.13.11
0.13.00
0.12.20
0.12.10
0.12.00
0.11.00
0.10.00
0.9.50
0.9.40
0.9.30
0.9.20
0.9.10
0.9.01
0.8.00
0.7.00
0.6.30
0.6.21
0.6.00
0.5.00
0.4.01
0.3.00
0.2.10
0.2.00

Package Sidebar

Install

npm i @connectedcars/test

Weekly Downloads

69

Version

2.0.1

License

MIT

Unpacked Size

3.02 MB

Total Files

271

Last publish

Collaborators

  • jagdos
  • mex
  • torbenkikkert
  • mtjcc
  • urcra
  • alparty
  • viter
  • chribsen
  • madspelt
  • sta-cc
  • aabnpm
  • lmfros
  • gbullock
  • bismlb
  • rljconnected
  • miafoo
  • emillykkeg
  • aakjaer