NervJS / nerv

A blazing fast React alternative, compatible with IE8 and React 16.

Home Page:https://nerv.aotu.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jest testing Nerv is not defined

zhanglong-3ti opened this issue · comments

测试代码

import Nerv, { findDOMNode } from 'nervjs';
import { Simulate, renderIntoDocument } from 'nerv-test-utils';

import { AccountEditPage } from './AccountEditPage';

const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

describe('<AccountEditPage />', () => {
   it('should render correctly', async () => {
        const component = renderIntoDocument(<AccountEditPage />);
        await delay(300);
        const items = findDOMNode(component, 'list-container').querySelectorAll('.list-item');
        const item0 = items[0];
        Simulate.click(item0);
        // expect(onClick).not.toBeCalled()
    });
});

转换成H5进行测试之后的代码:

import Taro from "@tarojs/taro-h5";
/* eslint-disable react/no-find-dom-node */
import { findDOMNode } from 'nervjs';
import { Simulate, renderIntoDocument } from 'nerv-test-utils';
import { AccountEditPage } from './AccountEditPage';
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
describe('<AccountEditPage />', () => {

  it('should render correctly', async () => {
    const component = renderIntoDocument(<AccountEditPage />);
    await delay(300);
    const items = findDOMNode(component, 'list-container').querySelectorAll('.list-item');
    const item0 = items[0];
    Simulate.click(item0);
    // expect(onClick).not.toBeCalled()
  });
});

转换之后 nervjs没把Nerv引入。

以下是我的babel

babel.config.js

const apis = require('@tarojs/taro-h5/dist/taroApis')

module.exports = {
  presets: [
    [
      '@babel/env',
      {

        spec: true,
        useBuiltIns: false,
      },
    ],
  ],
  plugins: [
    '@babel/plugin-proposal-class-properties',
    [
      '@babel/plugin-transform-react-jsx',
      {
        pragma: 'Nerv.createElement',
      },
    ],
    ['@babel/plugin-proposal-object-rest-spread'],
    [
      'babel-plugin-transform-taroapi',
      {
        apis,
        packageName: '@tarojs/taro-h5',
      },
    ]
  ],
}

你可以在测试环境把 pragma 换成 Taro.createElement 试试

你可以在测试环境把 pragma 换成 Taro.createElement 试试

我使用了Nerv.findDOMNode,自动引入 Nerv就可以了