Skip to content
This repository has been archived by the owner on Apr 18, 2022. It is now read-only.

Commit

Permalink
Upgrade deno to 1.11.0 and add assertion functions
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleJune committed Jun 13, 2021
1 parent 617fdc2 commit 874b56b
Show file tree
Hide file tree
Showing 28 changed files with 1,866 additions and 455 deletions.
10 changes: 10 additions & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
comment: false
codecov:
require_ci_to_pass: true
coverage:
status:
project:
default:
informational: true
ignore:
- examples
66 changes: 32 additions & 34 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,54 @@ name: CI
on: [push, pull_request]
jobs:
build:
name: ${{ matrix.config.kind }} ${{ matrix.config.os }}
runs-on: ${{ matrix.config.os }}
name: test deno ${{ matrix.deno }} ${{ matrix.os }}
runs-on: ${{ matrix.os }}
timeout-minutes: 5
strategy:
matrix:
config:
- os: macOS-latest
kind: test
- os: windows-latest
kind: test
- os: ubuntu-latest
kind: test
- os: ubuntu-latest
kind: lint
os: [ubuntu-latest, windows-latest, macOS-latest]
deno: [v1.x, canary]
fail-fast: true
steps:
- name: Clone repository
uses: actions/checkout@v2
- name: Install deno
uses: denolib/setup-deno@master
- name: Setup deno
uses: denoland/setup-deno@main
with:
deno-version: 1.9.0
deno-version: ${{ matrix.deno }}
- name: Check formatting
if: matrix.config.kind == 'lint'
run: |
deno fmt --check
deno lint --unstable
- name: Test
if: matrix.config.kind == 'test'
run: |
deno test
- name: Test unstable
if: matrix.config.kind == 'test'
run: |
deno test --coverage=cov_profile --unstable
- name: Test coverage
if: matrix.config.kind == 'test'
run: |
deno coverage --unstable cov_profile
if: matrix.os == 'ubuntu-latest'
run: deno fmt --check
- name: Check linting
if: matrix.os == 'ubuntu-latest'
run: deno lint
- name: Run tests
run: deno test --coverage=cov
- name: Run tests unstable
run: deno test --unstable
- name: Generate lcov
if: |
matrix.os == 'ubuntu-latest' &&
matrix.deno == 'v1.x'
run: deno coverage --lcov cov > cov.lcov
- name: Upload coverage
if: |
matrix.os == 'ubuntu-latest' &&
matrix.deno == 'v1.x'
uses: codecov/codecov-action@v1
with:
files: cov.lcov
- name: Release info
if: |
matrix.config.kind == 'test' &&
github.repository == 'udibo/mock' &&
matrix.os == 'ubuntu-latest' &&
matrix.deno == 'v1.x' &&
startsWith(github.ref, 'refs/tags/')
shell: bash
run: |
echo "RELEASE_VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
- name: Bundle
if: |
env.RELEASE_VERSION != '' &&
startsWith(matrix.config.os, 'ubuntu')
if: env.RELEASE_VERSION != ''
run: |
mkdir -p target/release
deno bundle mod.ts target/release/mock_${RELEASE_VERSION}.js
Expand Down
5 changes: 1 addition & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"deno.enable": true,
"deno.unstable": true,
"deno.unstable": false,
"deno.lint": true,
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
},
"deno.import_intellisense_origins": {
"https://deno.land": true
}
}
65 changes: 32 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Mock

[![version](https://img.shields.io/badge/release-v0.9.5-success)](https://github.com/udibo/mock/tree/v0.9.5)
[![deno doc](https://img.shields.io/badge/deno-doc-success?logo=deno)](https://doc.deno.land/https/deno.land/x/[email protected]/mod.ts)
[![deno version](https://img.shields.io/badge/deno-v1.9.0-success?logo=deno)](https://github.com/denoland/deno/tree/v1.9.0)
[![release](https://img.shields.io/badge/release-v0.10.0-success)](https://github.com/udibo/mock/releases/tag/v0.10.0)
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/[email protected]/mod.ts)
[![CI](https://github.com/udibo/mock/workflows/CI/badge.svg)](https://github.com/udibo/mock/actions?query=workflow%3ACI)
[![codecov](https://codecov.io/gh/udibo/mock/branch/master/graph/badge.svg?token=TXORMSEHM7)](https://codecov.io/gh/udibo/mock)
[![license](https://img.shields.io/github/license/udibo/mock)](https://github.com/udibo/mock/blob/master/LICENSE)

This module provides utilities to help mock behavior and spy on function calls
for tests.
Utilities to help mock behavior, spy on function calls, stub methods and fake
time for tests.

## Features

Expand All @@ -30,22 +30,22 @@ imported directly from GitHub using raw content URLs.

```ts
// Import from Deno's third party module registry
import { spy, Spy } from "https://deno.land/x/mock@v0.9.5/mod.ts";
import { spy, Spy } from "https://deno.land/x/mock@v0.10.0/mod.ts";
// Import from GitHub
import { spy, Spy } "https://raw.githubusercontent.com/udibo/mock/v0.9.5/mod.ts";
import { spy, Spy } "https://raw.githubusercontent.com/udibo/mock/v0.10.0/mod.ts";
```
If you do not need all of the sub-modules, you can choose to just import the
sub-modules you need.
```ts
// Import from Deno's third party module registry
import { Spy, spy } from "https://deno.land/x/mock@v0.9.5/spy.ts";
import { Spy, spy } from "https://deno.land/x/mock@v0.10.0/spy.ts";
// Import from GitHub
import {
Spy,
spy,
} from "https://raw.githubusercontent.com/udibo/mock/v0.9.5/spy.ts";
} from "https://raw.githubusercontent.com/udibo/mock/v0.10.0/spy.ts";
```

#### Sub-modules
Expand All @@ -69,15 +69,15 @@ If a Node.js package has the type "module" specified in its package.json file,
the JavaScript bundle can be imported as a `.js` file.

```js
import { Spy, spy } from "./mock_v0.9.5.js";
import { Spy, spy } from "./mock_v0.10.0.js";
```

The default type for Node.js packages is "commonjs". To import the bundle into a
commonjs package, the file extension of the JavaScript bundle must be changed
from `.js` to `.mjs`.

```js
import { Spy, spy } from "./mock_v0.9.5.mjs";
import { Spy, spy } from "./mock_v0.10.0.mjs";
```

See [Node.js Documentation](https://nodejs.org/api/esm.html) for more
Expand All @@ -96,15 +96,15 @@ modules must have the type attribute set to "module".

```js
// main.js
import { Spy, spy } from "./mock_v0.9.5.js";
import { Spy, spy } from "./mock_v0.10.0.js";
```

You can also embed a module script directly into an HTML file by placing the
JavaScript code within the body of the script tag.

```html
<script type="module">
import { spy, Spy } from "./mock_v0.9.5.js";
import { spy, Spy } from "./mock_v0.10.0.js";
</script>
```

Expand All @@ -120,7 +120,7 @@ a try block then restore the function in a finally block to ensure the original
instance method is restored before continuing to other tests. The same applies
when using fake time.

See [deno docs](https://doc.deno.land/https/deno.land/x/mock@v0.9.5/mod.ts) for
See [deno docs](https://doc.deno.land/https/deno.land/x/mock@v0.10.0/mod.ts) for
more information.

### Spy
Expand All @@ -135,8 +135,9 @@ anything, you can create an empty spy. An empty spy will just return undefined
for any calls made to it.

```ts
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { Spy, spy } from "https://deno.land/x/[email protected]/spy.ts";
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { assertSpyCall } from "https://deno.land/x/[email protected]/asserts.ts";
import { Spy, spy } from "https://deno.land/x/[email protected]/spy.ts";

function add(
a: number,
Expand All @@ -152,20 +153,18 @@ Deno.test("calls fake callback", () => {
const callback: Spy<void> = spy();

assertEquals(add(2, 3, callback), undefined);
assertSpyCall(callback, 1, { args: [undefined, 5] });
assertEquals(add(5, 4, callback), undefined);
assertEquals(callback.calls, [
{ args: [undefined, 5] },
{ args: [undefined, 9] },
]);
assertSpyCall(callback, 1, { args: [undefined, 9] });
});
```

If you have a function that takes a callback that needs to still behave
normally, you can wrap it with a spy.

```ts
import { assertEquals } from "https://deno.land/std@0.93.0/testing/asserts.ts";
import { Spy, spy } from "https://deno.land/x/mock@v0.9.5/spy.ts";
import { assertEquals } from "https://deno.land/std@0.98.0/testing/asserts.ts";
import { Spy, spy } from "https://deno.land/x/mock@v0.10.0/spy.ts";

function filter<T>(values: T[], callback: (value: T) => boolean): any[] {
return values.filter(callback);
Expand Down Expand Up @@ -196,8 +195,8 @@ method. If it is not restored and you attempt to wrap it again, it will throw a
spy error saying "already spying on function".

```ts
import { assertEquals } from "https://deno.land/std@0.93.0/testing/asserts.ts";
import { Spy, spy } from "https://deno.land/x/mock@v0.9.5/spy.ts";
import { assertEquals } from "https://deno.land/std@0.98.0/testing/asserts.ts";
import { Spy, spy } from "https://deno.land/x/mock@v0.10.0/spy.ts";

class Database {
private queries: any;
Expand Down Expand Up @@ -287,8 +286,8 @@ return values after initialization by replacing or adding to the `stub.returns`
queue. When the returns queue is empty, it will return undefined.

```ts
import { assertEquals } from "https://deno.land/std@0.93.0/testing/asserts.ts";
import { Stub, stub } from "https://deno.land/x/mock@v0.9.5/stub.ts";
import { assertEquals } from "https://deno.land/std@0.98.0/testing/asserts.ts";
import { Stub, stub } from "https://deno.land/x/mock@v0.10.0/stub.ts";

class Cat {
action(name: string): any {
Expand Down Expand Up @@ -324,8 +323,8 @@ them returned. You can add more return values after initialization by replacing
or adding to the `stub.returns` queue.

```ts
import { assertEquals } from "https://deno.land/std@0.93.0/testing/asserts.ts";
import { Stub, stub } from "https://deno.land/x/mock@v0.9.5/stub.ts";
import { assertEquals } from "https://deno.land/std@0.98.0/testing/asserts.ts";
import { Stub, stub } from "https://deno.land/x/mock@v0.10.0/stub.ts";

class Database {
query(query: string, params: any[]): any[][] {
Expand Down Expand Up @@ -400,8 +399,8 @@ initialization by replacing or adding to the `stub.returns` queue. When the
returns queue is empty, it will call the replacement function.

```ts
import { assertEquals } from "https://deno.land/std@0.93.0/testing/asserts.ts";
import { Stub, stub } from "https://deno.land/x/mock@v0.9.5/stub.ts";
import { assertEquals } from "https://deno.land/std@0.98.0/testing/asserts.ts";
import { Stub, stub } from "https://deno.land/x/mock@v0.10.0/stub.ts";

class Database {
query(query: string, params: any[]): any[][] {
Expand Down Expand Up @@ -476,9 +475,9 @@ Overrides the real Date object and timer functions with fake ones that can be
controlled through the fake time instance.

```ts
import { assertEquals } from "https://deno.land/std@0.93.0/testing/asserts.ts";
import { Spy, spy } from "https://deno.land/x/mock@v0.9.5/spy.ts";
import { FakeTime } from "https://deno.land/x/mock@v0.9.5/time.ts";
import { assertEquals } from "https://deno.land/std@0.98.0/testing/asserts.ts";
import { Spy, spy } from "https://deno.land/x/mock@v0.10.0/spy.ts";
import { FakeTime } from "https://deno.land/x/mock@v0.10.0/time.ts";

function secondInterval(cb: () => void): void {
setInterval(cb, 1000);
Expand Down
Loading

0 comments on commit 874b56b

Please sign in to comment.