createFile function

String createFile(
  1. String name,
  2. String contents
)

Creates arbitrary file in the same directory with the compiled test file.

This is useful for creating native Node modules (as well as any other fixtures) for testing interactions between Dart and JS.

Returns absolute path to the created file.

Example test case:

@JS()
@TestOn('node')
library pi_test;

import 'package:node_interop/test.dart';
import 'package:test/test.dart';
import 'package:js/js.dart';

/// Simple JS module which exports one value.
const fixtureJS = '''
exports.simplePI = 3.1415;
'''

@JS()
abstract class Fixture {
  external num get simplePI;
}

void main() {
  createFile('fixture.js', fixtureJS);

  test('simple PI', function() {
    Fixture fixture = require('./fixture.js');
    expect(fixture.simplePI, 3.1415);
  });
}

Implementation

String createFile(String name, String contents) {
  String script = process.argv[1];
  var segments = script.split(path.sep);
  segments
    ..removeLast()
    ..add(name);
  var jsFilepath = path.sep + segments.join(path.sep);
  fs.writeFileSync(jsFilepath, contents);
  return jsFilepath;
}