Skip to content

About

Run tests inside native apps built with Bare

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

bare-native-test

Run tests inside native apps built with Bare. Layout and drawing only happen while the platform event loop is running, so the tests are built into an app and run there. The module runs under both Bare and Node.js, and a CLI is also included.

npm i [-g] bare-native-test

bare-build is a peer dependency, so the app is built with the version installed alongside. The default runtime is bare-native/runtime, so bare-native is expected to be installed alongside as well, unless another runtime is passed.

Usage

Declare test cases with bare-tap:

test/window.js

const { test } = require('bare-tap')
const { Window } = require('bare-app-kit')

test('lays out the window', async (t) => {
  const window = new Window({ width: 300, height: 200 })
  t.teardown(() => window.close())

  window.makeKeyAndOrderFront()

  t.equal(window.contentView.frame.width, 300)
})

Require the test modules from an entry point:

test.js

require('./test/window')

Then run the entry point:

const run = require('bare-native-test')

const { ok } = await run('test.js', { runtime: 'bare-app-kit/runtime' })

The entry point is built into an app with https://github.com/holepunchto/bare-build for a device found with https://github.com/holepunchto/bare-device, and the output of the app is streamed as TAP. The run is judged by that output alone: it passes when the plan is complete and no test failed, however the app exits.

The app is installed with its runtime permissions already granted, so a test never waits on a permission dialog. On Android they still have to be declared, which the default manifest does not do, so a test that needs one passes a manifest of its own as android: { manifest } with --build-config, described below.

An iOS device only runs a signed app. bare-build signs an app for one by default, but a run on one still needs a provisioning profile, and an identifier the profile covers. They are read from a module:

test/build.js

module.exports = ({ device }) => {
  if (device.platform !== 'ios') return {}

  return {
    identifier: 'to.holepunch.bare.native.test',
    ios: {
      provisioningProfile: 'test/Tests.mobileprovision',
      infoPlist: 'test/Info.plist'
    }
  }
}

The module exports either an object of options, or a function that returns one. The function is called with { device }, the device the app is built for, so the options can differ per platform. Paths in the options are relative to the module. The run sets base, hosts, runtime, out and format itself, and joins defer with the specifiers given to --defer.

bare-native-test --platform ios --device "iPhone" --build-config test/build.js test.js

A run that needs something done on this machine around the app, such as driving the device while the tests watch it, gets --before and --after. The before hook runs once the app is installed, after once the run is over, even when it bailed, and receives whatever before resolved with.

test/before.js

module.exports = async function ({ device }) {
  await device.shell('cmd', ['bluetooth_manager', 'disable'])
}
bare-native-test --platform android --before test/before.js --after test/after.js test.js

--env sets an environment variable for the app, on this machine only:

bare-native-test --env BARE_LOG=debug test.js

--env @<path> reads them from a file instead, and --env - from stdin, one NAME=VALUE per line. Values are taken as they are, without quotes or escapes, and lines that are blank or start with # are skipped. Variables are applied in order, so a later --env takes precedence. This takes the environment that bare-make test env prints, such as the preloaded sanitizer runtime of a build with --sanitize address:

bare-make test env | bare-native-test --env - test.js
bare-make test env > test.env
bare-native-test --env @test.env test.js

CLI

bare-native-test [flags] <entry>

Build the tests at <entry> into an app for a device, launch it there, and stream its TAP output. Exits with 0 if the output reports a complete run with no failures, and 1 otherwise.

--version|-v
--build-config <path>
--before <path>
--after <path>
--platform <name>
--device|-d <name>
--runtime <specifier>
--out|-o <dir>
--defer <specifier>
--env <NAME=VALUE>
--timeout <ms>
--help|-h
Flag Default Description
--build-config Nothing A module exporting further bare-build options
--before Nothing A module to run on this machine before the app is launched
--after Nothing A module to run on this machine after the run
--platform The current platform The platform to run on, such as darwin or ios
--device This machine, or a running device The name of the device to run on
--runtime bare-native/runtime The runtime to build the app with
--out build/test Where to write the entry point and the app
--defer Nothing A module specifier to defer resolution of
--env Nothing An environment variable to set for the app, or @<path> or - to read them
--timeout 5 minutes Give up on a run that stops making progress for this long

License

Apache-2.0

About

Run tests inside native apps built with Bare

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages