0.240.0 • Published 1 year ago

@teamkeel/testing v0.240.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

@teamkeel/testing

** All examples use the following sample schema:

model Post {
  fields {
    title Text
  }

  operations {
    create createPost() with(title) {
      @permission(post.title != "bar")
    }
  }
}

api Web {
  @graphql

  models {
    Post
  }
}

Overview

The @teamkeel/testing package allows us to write end-to-end tests for the Keel runtime.

Our internal tests are located in the integration/testdata directory. The integration test framework is a "meta test", meaning that in addition to any assertions we make in our *.test.ts, there is a corresponding expected.json file that documents which test cases should pass or fail. This is especially useful if we have test cases we intentionally want to fail.

Each directory is its own isolated application. At the very least, a test case directory should have:

  • A schema.keel file
  • At least one test file, following the glob pattern *.test.ts
  • An expected.json file, which documents which test cases should pass or fail

If you are testing custom functions, then you should include a functions/ sub directory, with custom function files inside.

Define new test cases using:

import { test } from "@teamkeel/testing";

test("it does something", async () => {});

The testing framework allows you to setup data in the database, call both built-in and custom actions, as well as assert on the return value of actions, fulfilling the AAA (arrange-act-assert) testing strategy:

import { test, expect, Actions, MyModel } from "@teamkeel/testing";

test("a sample test", async () => {
  // arrange - setup the data
  const { object: createdModel } = await MyModel.create({ foo: "bar" });

  // you can also fetch things from the database:
  const { collection } = await MyModel.findMany({ foo: "bar" });
  const { object } = await MyModel.findOne({ myUniqueField: "xxx" });

  // act - call an action
  const { object: result } = await Actions.getMyModel({ id: createdModel.id });

  // assert on the result
  expect(result.id).toEqual(createdModel.id);
});

Querying the database

You can create / update and query data easily using our Database API. Complete Database API documentation can be found at XXX

import { test, Post } from "@teamkeel/testing";

test("it does something", async () => {
  const { object: createdPost } = await Post.create({
    title: "a title",
  });

  expect(createdPost.title).toEqual("a title");
});

Calling actions

You can execute Keel actions like so:

import { test, actions } from "@teamkeel/testing";

test("it does something", async () => {
  const { object: createdPost } = await actions.createPost({
    title: "a title",
  });

  expect(createdPost.title).toEqual("a title");
});

Expectation API

The table below outlines our expectation API:

ExpectationActual typeExpected typeExample
toEqualanyanyexpect(1).toEqual(1)
notToEqualanyanyexpect(1).notToEqual(2)
toHaveErrorActionResultActionErrorexpect(await actions.createPost({ title: 'too long' })).toHaveError({ message: 'too long' })
notToHaveErrorActionResultActionErrorexpect(await actions.createPost({ title: 'OK' })).notToHaveError()
toHaveAuthorizationErrorActionResultN/Aexpect(await actions.createPost({ title: 'bar' })).toHaveAuthorizationError()
notToHaveAuthorizationErrorActionResultN/Aexpect(await actions.createPost({ title: 'foo' })).notToHaveAuthorizationError()
toBeEmptyanyN/Aexpect(null).toBeEmpty()
notToBeEmptyanyN/Aexpect({ foo: 'bar' }).notToBeEmpty()
toContainArray<T>anyexpect([{ foo: 'bar' }]).toContain({ foo: 'bar' })
notToContainArray<T>anyexpect([]).notToContain({ foo: 'bar' })

Running integration tests

To run all of the integration tests, you can simply run:

make testintegration

Running specific integration tests

Via the command line

If you want to run all TypeScript tests in a particular test case directory, you can run:

go test ./integration -run ^TestIntegration/my_test_case_dir_name -count=1 -v

However, this doesn't allow you to isolate an individual test case written in a test() block in a *.test.ts file. In order to do this, you must pass an additional pattern flag to the test command like so:

go test ./integration -run ^TestIntegration/built_in_actions -count=1 -v -pattern "list action"

The pattern can either be a simple string matching a test case name, or you can provide a regex:

go test ./integration -run ^TestIntegration/built_in_actions -count=1 -v -pattern "get action (.*)"

The above will run all tests that begin with "get action".

Running / debugging tests via vscode test runner

You can use the Run / Debug integration test launch configuration in VSCode. When you click the "Play" button, it will ask you to input the test case directory (relative from the root integration/testdata) as well as to input any test name patterns which allows you to either isolate on an individual JavaScript/TypeScript test or you can provide a Regular Expression.

This allows you to debug individual tests using the VSCode debugger, although you will not be able to debug the TypeScript test code at the moment - it is possible in the future we may be able to do this as we can pass additional flags to enable debugging to ts-node when running a test file via the test harness.

Gotchas

  • The test runner actually copies each test case directory to a temporary directory elsewhere when executing the test - inside of the temporary directory, all of the dependencies necessary to have a viable application are installed (this also includes creation of package.json and tsconfig.json). This saves us having to populate each directory each time we add a new test case. The downside is that we do not get intellisense when writing our tests; this would be tricky anyway given a lot of the code inside of the @teamkeel/testing package is code generated at the point of running the test.
0.236.3

1 year ago

0.236.0

1 year ago

0.236.2

1 year ago

0.236.1

1 year ago

0.240.0

1 year ago

0.237.0

1 year ago

0.234.0

1 year ago

0.235.4

1 year ago

0.235.1

1 year ago

0.235.0

1 year ago

0.235.3

1 year ago

0.235.2

1 year ago

0.228.3

1 year ago

0.232.0

1 year ago

0.232.1

1 year ago

0.229.0

1 year ago

0.233.0

1 year ago

0.238.0

1 year ago

0.230.1

1 year ago

0.230.0

1 year ago

0.239.0

1 year ago

0.231.0

1 year ago

0.228.4

1 year ago

0.209.0

1 year ago

0.224.1

1 year ago

0.190.0

2 years ago

0.224.0

1 year ago

0.224.3

1 year ago

0.224.2

1 year ago

0.201.0

2 years ago

0.152.0

2 years ago

0.175.0

2 years ago

0.198.0

2 years ago

0.175.1

2 years ago

0.213.0

1 year ago

0.213.1

1 year ago

0.148.0

2 years ago

0.163.0

2 years ago

0.186.0

2 years ago

0.225.0

1 year ago

0.202.0

2 years ago

0.225.1

1 year ago

0.159.1

2 years ago

0.159.0

2 years ago

0.197.0

2 years ago

0.174.0

2 years ago

0.151.0

2 years ago

0.214.1

1 year ago

0.214.0

1 year ago

0.147.2

2 years ago

0.147.1

2 years ago

0.147.0

2 years ago

0.162.1

2 years ago

0.185.0

2 years ago

0.162.0

2 years ago

0.207.0

1 year ago

0.222.3

1 year ago

0.222.2

1 year ago

0.192.0

2 years ago

0.222.4

1 year ago

0.222.1

1 year ago

0.222.0

1 year ago

0.177.0

2 years ago

0.154.0

2 years ago

0.177.1

2 years ago

0.219.0

1 year ago

0.180.0

2 years ago

0.211.0

1 year ago

0.188.0

2 years ago

0.165.0

2 years ago

0.208.0

1 year ago

0.208.1

1 year ago

0.191.0

2 years ago

0.191.1

2 years ago

0.200.0

2 years ago

0.223.0

1 year ago

0.199.3

2 years ago

0.199.4

2 years ago

0.199.5

2 years ago

0.199.6

2 years ago

0.153.0

2 years ago

0.199.0

2 years ago

0.176.0

2 years ago

0.199.1

2 years ago

0.199.2

2 years ago

0.212.0

1 year ago

0.149.0

2 years ago

0.164.0

2 years ago

0.187.0

2 years ago

0.228.1

1 year ago

0.228.0

1 year ago

0.205.1

2 years ago

0.228.2

1 year ago

0.205.0

2 years ago

0.171.0

2 years ago

0.220.0

1 year ago

0.179.1

2 years ago

0.194.0

2 years ago

0.156.0

2 years ago

0.179.0

2 years ago

0.217.0

1 year ago

0.217.1

1 year ago

0.182.0

2 years ago

0.167.0

2 years ago

0.206.0

2 years ago

0.206.1

2 years ago

0.221.4

1 year ago

0.221.3

1 year ago

0.193.0

2 years ago

0.170.0

2 years ago

0.221.0

1 year ago

0.221.2

1 year ago

0.221.1

1 year ago

0.178.0

2 years ago

0.155.0

2 years ago

0.218.0

1 year ago

0.181.0

2 years ago

0.210.0

1 year ago

0.166.1

2 years ago

0.189.0

2 years ago

0.166.0

2 years ago

0.226.0

1 year ago

0.203.0

2 years ago

0.158.0

2 years ago

0.173.0

2 years ago

0.196.0

2 years ago

0.150.0

2 years ago

0.215.0

1 year ago

0.215.1

1 year ago

0.169.0

2 years ago

0.184.0

2 years ago

0.161.0

2 years ago

0.146.0

2 years ago

0.204.0

2 years ago

0.227.0

1 year ago

0.195.0

2 years ago

0.172.0

2 years ago

0.157.0

2 years ago

0.216.0

1 year ago

0.160.0

2 years ago

0.183.1

2 years ago

0.183.0

2 years ago

0.168.0

2 years ago

0.118.0

2 years ago

0.137.0

2 years ago

0.114.0

2 years ago

0.110.0

2 years ago

0.133.1

2 years ago

0.133.0

2 years ago

0.106.1

2 years ago

0.102.5

2 years ago

0.106.2

2 years ago

0.102.6

2 years ago

0.106.3

2 years ago

0.102.7

2 years ago

0.125.0

2 years ago

0.106.4

2 years ago

0.102.8

2 years ago

0.102.1

2 years ago

0.102.2

2 years ago

0.102.3

2 years ago

0.129.0

2 years ago

0.106.0

2 years ago

0.102.4

2 years ago

0.140.0

2 years ago

0.144.0

2 years ago

0.121.0

2 years ago

0.106.5

2 years ago

0.102.9

2 years ago

0.106.6

2 years ago

0.106.7

2 years ago

0.136.0

2 years ago

0.117.0

2 years ago

0.113.0

2 years ago

0.132.0

2 years ago

0.128.0

2 years ago

0.102.17

2 years ago

0.102.16

2 years ago

0.109.0

2 years ago

0.102.15

2 years ago

0.102.14

2 years ago

0.102.13

2 years ago

0.102.12

2 years ago

0.105.0

2 years ago

0.102.11

2 years ago

0.102.10

2 years ago

0.124.0

2 years ago

0.143.0

2 years ago

0.120.0

2 years ago

0.102.19

2 years ago

0.102.18

2 years ago

0.139.0

2 years ago

0.112.2

2 years ago

0.116.0

2 years ago

0.102.21

2 years ago

0.131.0

2 years ago

0.102.20

2 years ago

0.112.0

2 years ago

0.112.1

2 years ago

0.135.0

2 years ago

0.127.0

2 years ago

0.108.0

2 years ago

0.108.1

2 years ago

0.104.0

2 years ago

0.104.1

2 years ago

0.142.0

2 years ago

0.123.0

2 years ago

0.138.0

2 years ago

0.119.0

2 years ago

0.111.3

2 years ago

0.115.0

2 years ago

0.111.4

2 years ago

0.111.5

2 years ago

0.111.6

2 years ago

0.130.0

2 years ago

0.111.0

2 years ago

0.111.1

2 years ago

0.111.2

2 years ago

0.134.0

2 years ago

0.107.0

2 years ago

0.126.0

2 years ago

0.103.0

2 years ago

0.141.1

2 years ago

0.141.0

2 years ago

0.145.1

2 years ago

0.145.0

2 years ago

0.122.0

2 years ago

0.95.8

2 years ago

0.95.9

2 years ago

0.98.1

2 years ago

0.95.4

2 years ago

0.99.1

2 years ago

0.98.2

2 years ago

0.95.5

2 years ago

0.98.3

2 years ago

0.95.6

2 years ago

0.95.7

2 years ago

0.95.0

2 years ago

0.96.0

2 years ago

0.95.1

2 years ago

0.97.0

2 years ago

0.96.1

2 years ago

0.95.10

2 years ago

0.95.2

2 years ago

0.98.0

2 years ago

0.97.1

2 years ago

0.96.2

2 years ago

0.95.3

2 years ago

0.100.3

2 years ago

0.100.0

2 years ago

0.101.0

2 years ago

0.100.1

2 years ago

0.102.0

2 years ago

0.100.2

2 years ago

0.94.0

2 years ago

0.93.1

2 years ago