Skip to content

e280/science

Repository files navigation

🧪 @e280/science

  • minimalist ts/js testing framework
  • deadass simple, no cli actually
  • no funky instrumentation horseshit
  • zero dependencies
  • an https://e280.org/ project

Easy setup

  • install science
    npm install --save-dev @e280/science
  • create your tests.test.js
    import {Science, suite, test, expect} from "@e280/science"
    
    await Science.run({
      "addition works": test(async() => {
        expect(2 + 2).is(4)
      }),
    })
  • run your tests in node
    node tests.test.js
  • watch mode (run-on-save!)
    node --watch tests.test.js
  • run the debugger
    node inspect tests.test.js
  • stick it in your package.json
    "scripts": {
      "test": "node tests.test.js",
      "test-watch": "node --watch tests.test.js"
    },

Learn by example

Happy tests

// example test case
"addition works": test(async() => {
  expect(2 + 2).is(4)
}),


Skipping tests

  //            skip this test
  //                   👇
"addition works": test.skip(async() => {
  expect(2 + 2).is(4)
}),


Only running some tests

  //            only run this test
  //                   👇
"addition works": test.only(async() => {
  expect(2 + 2).is(4)
}),


Failing tests

"addition works": test(async() => {

  // fail by expectation
  expect(2 + 2).is(5)

  // fail by returning false
  return false

  // fail by throwing a string or error
  throw "universe is broken"
}),


Arbitrary nesting of test suites

await Science.run({
  "nesting": suite({
    "deeper nesting": suite({
      "addition works": test(async() => {
        expect(2 + 2).is(4)
      }),
    }),
  }),
})
  • suite.skip works
  • suite.only works

Passing in options

  • the options object passed in via javascript gets top priority
    await Science.run(myTestSuite, {
    
      // print all test cases
      verbose: true,
    
      // disable colors and emojis
      theme: Science.themes.plain,
    })
  • the next fallback are cli arguments
    node tests.test.js --verbose --theme=seaside
  • (best practice) the next fallback are environment vars
    SCIENCE_VERBOSE=1 SCIENCE_THEME=seaside node tests.test.js
    • using environment variables is preferable to hard-coding anything
    • this allows developers to choose their own preference
    • they can simply run export SCIENCE_VERBOSE=1 or export SCIENCE_THEME=seaside in their terminal before starting a science watch routine

Available themes

  • redgreen (default) errors are red, happy tests are green
  • seaside better for color blindness, errors are red, happy tests are blue
  • plain no colors, no emojis


The key to happiness is realistic expectations

see all the expectations in expectations.ts

expect(2 + 2).is(4)

// custom fail note
expect(2 + 2, "universe is broken").is(2)

// custom fail note (alt syntax)
expect(2 + 2)
  .note("universe is broken")
  .is(2)

expect(2 + 2).isnt(4)
expect(2 + 2).gt(3)
expect(2 + 2).lt(5)
expect(2 + 2).gte(4)
expect(2 + 2).lte(4)

expect(() => {throw "lol"}).throws()
await expect(async() => {throw "lol"}).throwsAsync()

// you can ".not" anything
expect(2 + 2).not.is(5)
expect(2 + 2).not.isnt(4) // lol
expect(() => {throw "lol"}).not.throws()

💖 Made with open source love

build with us at https://e280.org/ but only if you're cool

About

simple test framework

Resources

License

Stars

Watchers

Forks

Packages

No packages published