Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UX Improvements #273

Merged
merged 73 commits into from
Jun 2, 2022
Merged

UX Improvements #273

merged 73 commits into from
Jun 2, 2022

Conversation

dimitrovmaksim
Copy link
Member

@dimitrovmaksim dimitrovmaksim commented Jan 11, 2022

Issues:
closes #119
closes #310

Depends on:
LimeChain/matchstick-as#43

What it does:

  • Adds beforeAll and afterAll functions
  • Adds beforeEach and afterEach functions
  • Adds describe with support for nested describes
  • Fixes some issues with the logic for detecting if imported files have been changed.
  • Now every test.ts file will be compiled into a separate test suite, even if multiple test files are grouped into a folder, e.g:
tests/
└── gravity
    ├── create.test.ts
    └── update.test.ts

1 directory, 2 files

will yield two test suites named gravity/create and gravity/update

  • Updates running only the tests that were specified with a pattern
graph test gravity # this will run all test that contain the word gravity
  1. Basic Structure:
  2. How to test

Basic Structure:

describe(name: String , () => {}) - Defines an example/test group.

Notes:

  • Describes are not mandatory. You can still use test() outside of describe() blocks

Example:

import {describe, test } from "matchstick-as/assembly/index"

describe(“Example test group”, () => {
    test(“A test case”, () => {…})
	
    test(“Another test case”, () => {…})
})

Nested describe() example:

import {describe, test } from "matchstick-as/assembly/index"

describe(“Outside describe”, () => {
  describe("Inner describe", () => {
     ...

     test(“A test case”, () => {…})

     ...
  }
   
  describe("Another Inner describe", () => {
     ...

     test(“Another test case”, () => {…})

     ...
  }
})

test(name: String, () =>, should_fail: bool) - Defines an example/test case. You can use test() inside describe() blocks or independently.

Example:

import { describe, test } from "matchstick-as/assembly/index"

describe(“Example test group”, () => {
    test(“Example test in a group”, () => {…}) // This case is expected to succeed.
    ...
})

test(“Independent test with should_fail set to true”, () => {…}, true) // This case is expected to fail. 



beforeAll(() => {}) - Runs a code block before any of the tests in the file. If beforeAll is inside a describe block, it runs at the beginning of the describe block.

Examples:

Code inside beforeAll will execute once before all describe blocks

import {describe, test, beforeAll } from "matchstick-as/assembly/index"

beforeAll(() => {
    …
})

describe(“Example test group”, () => {
    …
})

describe(“Another test group”, () => {
    …
})

Code inside beforeAll will execute once before all tests in the first describe block

import {describe, test, beforeAll } from "matchstick-as/assembly/index"


describe(“Example test group”, () => {
    beforeAll(() => {
        …
    })

    test(“Example test case”, () => {…})
    test(“Another test case”, () => {…})
})

describe(“Another test group”, () => {
    …
})

afterAll(() => {}) - Runs a code block after all of the tests in the file. If afterAll is inside a describe block, it runs at the end of the describe block.

Example:

Code inside afterAll will execute once after all describe blocks

import {describe, test, afterAll } from "matchstick-as/assembly/index"

afterAll(() => {
    …
})

describe(“Example test group”, () => {
    …
})

describe(“Another test group”, () => {
    …
})

Code inside afterAll will execute once after all tests in the first describe block

import {describe, test, afterAll } from "matchstick-as/assembly/index"


describe(“Example test group”, () => {
	afterAll(() => {
		…
	})

    test(“Example test case”, () => {…})
    test(“Another test case”, () => {…})
})

describe(“Another test group”, () => {
	…
})

beforeEach(() => {}) - Runs a code block before every test. If beforeEach is inside a describe block, it runs before each test in that describe.

Examples:
Code inside beforeEach will execute before each tests.

import {describe, test, beforeEach } from "matchstick-as/assembly/index"

beforeEach(() => {
    …
})

describe(“Example test group”, () => {
    …
})

describe(“Another test group”, () => {
    …
})

Code inside beforeEach will execute before each test in the first describe

import {describe, test, beforeEach } from "matchstick-as/assembly/index"


describe(“Example test group”, () => {
	beforeEach(() => {
		…
	})

    test(“Example test case”, () => {…})
    test(“Another test case”, () => {…})
})

describe(“Another test group”, () => {
	…
})

afterEach(() => {}) - Runs a code block after every test. If afterEach is inside a describe block, it runs after each test in that describe.

Examples:

Code inside afterEach will execute after every test.

import {describe, test, afterEach } from "matchstick-as/assembly/index"

afterEach(() => {
    …
})

describe(“Example test group”, () => {
    …
})

describe(“Another test group”, () => {
    …
})

Code inside afterEach will execute after each test in the first describe

import {describe, test, afterEach } from "matchstick-as/assembly/index"


describe(“Example test group”, () => {
    afterEach(() => {
        …
    })

    test(“Example test case”, () => {…})
    test(“Another test case”, () => {…})
})

describe(“Another test group”, () => {
	…
})

How To Test:

There is a beta release if you want to try it out, but you'll need a couple of things before that:

  1. `matchstick-as: 0.5.0.
  2. You'll need to use matchstick v 0.5.0. You can do that either by providing the -v flag to the graph test command graph test -v 0.5.0, or you can download the binary for your OS, to your subgraph folder, change the permissions with chmod a+x and then run the binary from the terminal, for example ./binary-macos-11

That should be all!

If you encounter any bug or issues, or have any questions, feel free to leave a comment.

@dimitrovmaksim dimitrovmaksim changed the title UX Improvements UX Improvements - Do not merge May 9, 2022
@dimitrovmaksim dimitrovmaksim marked this pull request as ready for review May 9, 2022 12:12
@dimitrovmaksim dimitrovmaksim changed the title UX Improvements - Do not merge UX Improvements May 27, 2022
}
}

// A recursive fucntion that builds the the test suite from a single test.ts file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fucn


// In order to get all functions inside a describe() block, we need to create a clone
// of the current MatchstickInstance, then fetch and execute the said describe() function by it's id
// from the context of the cloned instance. This will trigger the reigstration of all the functions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reig

@dimitrovmaksim dimitrovmaksim merged commit 7e13adb into main Jun 2, 2022
@georg-getz georg-getz deleted the test_ux_improvements branch June 9, 2022 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nested describes Improve UX for writing tests
2 participants