1.4.0 • Published 11 days ago

@connectedcars/test v1.4.0

Weekly downloads
343
License
MIT
Repository
github
Last release
11 days ago

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
  })
})
1.4.0

11 days ago

1.2.4-0

1 month ago

1.3.1

2 months ago

1.3.0

2 months ago

1.2.3

2 months ago

1.2.2-1

2 months ago

1.2.2-3

2 months ago

1.2.2-2

2 months ago

1.2.2

2 months ago

1.2.0

3 months ago

1.2.1

3 months ago

1.1.0

4 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.10

9 months ago

1.0.5

11 months ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

0.22.9

1 year ago

0.22.8

1 year ago

0.22.7

1 year ago

0.22.6

1 year ago

0.22.5

1 year ago

0.22.4

2 years ago

0.22.3

2 years ago

0.22.2

2 years ago

0.22.1

2 years ago

0.22.0

2 years ago

0.21.0

2 years ago

0.20.6

2 years ago

0.21.1

2 years ago

0.20.5

2 years ago

0.20.4

2 years ago

0.20.3

3 years ago

0.20.2

3 years ago

0.20.1

3 years ago

0.20.0

3 years ago

0.19.0

3 years ago

0.18.1

3 years ago

0.18.2

3 years ago

0.18.0

3 years ago

0.17.1

3 years ago

0.17.0

3 years ago

0.16.2

3 years ago

0.16.1

3 years ago

0.16.0

3 years ago

0.14.0

3 years ago

0.15.0

3 years ago

0.13.1

3 years ago

0.13.0

3 years ago

0.12.2

3 years ago

0.12.1

3 years ago

0.12.0

3 years ago

0.11.0

3 years ago

0.10.0

4 years ago

0.9.5

4 years ago

0.9.4

4 years ago

0.9.3

4 years ago

0.9.2

4 years ago

0.9.1

4 years ago

0.9.0

4 years ago

0.8.0

4 years ago

0.7.0

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.0

4 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago