Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ rec {

dfx = import ./dfx.nix { inherit pkgs userlib-js; };

e2e-tests = import ./e2e { inherit pkgs dfx; };
e2e-tests = import ./e2e/bats { inherit pkgs dfx; };
node-e2e-tests = import ./e2e/node { inherit pkgs dfx; };

userlib-js = import ./src/userlib/js { inherit pkgs; };

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions e2e/default.nix → e2e/bats/default.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{ pkgs ? import ../nix { inherit system; }
{ pkgs ? import ../../nix { inherit system; }
, system ? builtins.currentSystem
, dfx ? import ../dfx.nix { inherit pkgs; }
, dfx ? import ../../dfx.nix { inherit pkgs; }
}:
let
e2e = lib.noNixFiles (lib.gitOnlySource ../. "e2e");
e2e = lib.noNixFiles (lib.gitOnlySource ../../. ./.);
lib = pkgs.lib;
sources = pkgs.sources;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
55 changes: 55 additions & 0 deletions e2e/node/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
= Node End-to-End Tests

== High level overview

These tests are composed of two parts;

1. a Nix script that installs the Userlib in the execution location, as if it was an NPM
package. This is done by building the NPM Package, and the JS Userlib derivation outputting
2 outputs; the NPM package itself (in a packed format), and its node_modules which are
then copied in the execution folder for these tests.
2. a Jest framework in TypeScript that runs and validate the tests.

The result is you build your tests as you would with Jest. You can import the JavaScript
userlib by using `import * as userlib from '@internet-computer/userlib'` which is as
close as it can be to the real world.

== Starting / Stopping the Replica

The `setup` Jest step starts the replica using `dfx replica`. The `teardown` step sends
a SIGTERM to the `dfx` process. Nix will wait for all children and descendants to be
stopped, so Nix will timeout and fail if the replica isn't properly killed.

== Adding New Tests

Adding a test is just a matter of having a file with a `.ts` extension anywhere outside
the root directory of tests and the `utils/` folder. This is driven by the
`jest.config.js` file which has a pattern matching all those files.

We don't force users to have a `.test.ts` extension as this whole folder is meant to be
tests. It's just simpler that way.

== Using HttpAgent

The `utils/` folder contain an `agent.ts` file which exports an `HttpAgent` instance
which connects to the testing Replica. Unless required by your test to have a special
agent configuration, you should always reuse that one.

== Creating and Using Canisters

Right now, the way to add a canister is to manually build it and check in the canisters
WASM. This is a limitation of the current setup and will (hopefully) go away soon.

Once the WASM is checked in, a canister `.ts` should be added which exports a factory.
That factory reads the WASM, creates an actor with a random canisterId, builds the IDL
manually (currently) and install the canister on the Replica. A good example of this
can be seen in `utils/canisters/counter.ts`.

= TODOs

- [ ] Adding linting and prettier to standardize syntax.
- [ ] Add a dfx project derivation which generates the WASM and IDL through Nix
so it doesn't have to be checked in.
- [ ] Also, reuse canister IDs exposed by the dfx project derivation above instead
of generating a random one. This is important for testing canisters which
have inter-canister calls.
11 changes: 11 additions & 0 deletions e2e/node/basic/call.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import counterFactory from '../utils/canisters/counter';

test('can call a canister', async () => {
let counter = await counterFactory();

expect(+(await counter.read())).toEqual(0);
expect(+(await counter.inc_read())).toEqual(1);
await counter.write(10);
expect(+(await counter.read())).toEqual(10);
expect(+(await counter.inc_read())).toEqual(11);
});
46 changes: 46 additions & 0 deletions e2e/node/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{ pkgs ? import ../../nix { inherit system; }
, system ? builtins.currentSystem
, dfx ? import ../../dfx.nix { inherit pkgs; }
, userlib-js ? import ../../src/userlib/js { inherit pkgs; }
}:
let
e2e = pkgs.lib.noNixFiles (pkgs.lib.gitOnlySource ../../. ./.);
inputs = with pkgs; [
coreutils
dfx.standalone
nodejs-12_x
];
in

pkgs.napalm.buildPackage e2e {
root = ./.;
name = "node-e2e-tests";
buildInputs = inputs;
PATH = pkgs.lib.makeSearchPath "bin" inputs;

npmCommands = [
"npm install"

# Monkey-patch the userlib source into our install dir. napalm is unable
# to include dependencies from package-locks in places other than the
# build root.
(
pkgs.writeScript "include-userlib.sh" ''
#!${pkgs.stdenv.shell}
set -eo pipefail

userlib="node_modules/@internet-computer/userlib"
mkdir -p $userlib

tar xvzf ${userlib-js.out}/internet-computer-*.tgz --strip-component 1 --directory $userlib/
cp -R ${userlib-js.lib}/node_modules .
''
)
"npm run ci"
];

installPhase = ''
echo Done.
touch $out
'';
}
23 changes: 23 additions & 0 deletions e2e/node/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
bail: false,
testTimeout: 60000,
globalSetup: './setup',
globalTeardown: './teardown',
setupFiles: [
"./test-setup",
],
setupFilesAfterEnv: [
"jest-expect-message",
],
// Since we're running e2e tests, ALL typescript files are up for grab.
testMatch: [
"**/*.ts"
],
testPathIgnorePatterns: [
"/node_modules/",
"/utils/",
],
transform: {
"^.+\\.ts$": "ts-jest"
}
};
Loading