最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - using flowtype to statically check mocha test code - Stack Overflow

programmeradmin0浏览0评论

I have some plex Mocha code which I would like to statically check with FlowType because why not?

Below is a minimal repro:

/* @flow */

describe('it', function () {
    it('fails', function() {
        const s: number = 'flow spots this error';
    });
});

When I run Flow on this, Flow does indeed spot the problem with the assignment of string to number which shows that the approach is working to some extend.

However, I also get:

test/test.js:4
  4: describe('it', function () {
     ^^^^^^^^ identifier `describe`. Could not resolve name

test/test.js:5
  5:     it('fails', function() {
         ^^ identifier `it`. Could not resolve name

… apparently the Mocha test definitions run in an environment where these functions are globally available but looking at the test file there's nothing that would allow Flow to detect that.

I am not sure these problems are specific to Mocha but I don't feel I can confidently frame the question in broader terms, so my questions are:

  1. how can I have Flow type check Mocha test code without suppressing every line that contains describe or it ?
  2. is this is an instance of a broader class of situations and, if so, what would the latter be?

I have some plex Mocha code which I would like to statically check with FlowType because why not?

Below is a minimal repro:

/* @flow */

describe('it', function () {
    it('fails', function() {
        const s: number = 'flow spots this error';
    });
});

When I run Flow on this, Flow does indeed spot the problem with the assignment of string to number which shows that the approach is working to some extend.

However, I also get:

test/test.js:4
  4: describe('it', function () {
     ^^^^^^^^ identifier `describe`. Could not resolve name

test/test.js:5
  5:     it('fails', function() {
         ^^ identifier `it`. Could not resolve name

… apparently the Mocha test definitions run in an environment where these functions are globally available but looking at the test file there's nothing that would allow Flow to detect that.

I am not sure these problems are specific to Mocha but I don't feel I can confidently frame the question in broader terms, so my questions are:

  1. how can I have Flow type check Mocha test code without suppressing every line that contains describe or it ?
  2. is this is an instance of a broader class of situations and, if so, what would the latter be?
Share Improve this question asked Nov 17, 2016 at 22:57 Marcus Junius BrutusMarcus Junius Brutus 27.3k46 gold badges202 silver badges350 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Third-party libraries usually need definition files, i.e. files containing all the type information for a given library.

In this case, you need a definition file for mocha, which fortunately is provided by flow-typed.

Install it with

npm install -g flow-typed

then run

flow-typed install 

It will automatically install all the available definition files for your dependencies, including mocha.

You can simply declare the flow describe, it variables.

/* @flow */
declare var describe: any;
declare var it: any;

describe('it', function () {
    it('fails', function() {
        const s: number = 'flow spots this error';
    });
});
发布评论

评论列表(0)

  1. 暂无评论