小编3Do*_*Dos的帖子

开玩笑不等待所有解决方案开始测试

我测试的内容:快速服务器端点

我的目标:在单个脚本中自动化 API 测试

我做什么:我在 NodeJS 子进程中启动 express 服务器,并希望在运行测试套件之前等待它启动(frisby.js 端点测试)

什么没有按预期工作:在 Promise 解决之前启动测试套件

wait-on一旦资源可用,我依赖于服务器轮询和解析的包。

const awaitServer = async () => {
  await waitOn({
    resources: [`http://localhost:${PORT}`],
    interval: 1000,
  }).then(() => {
    console.log('Server is running, launching test suite now!');
  });
};
Run Code Online (Sandbox Code Playgroud)

这个函数用在startServer函数中:

const startServer = async () => {
  console.log(`Launching server http://localhost:${PORT} ...`);

  // npmRunScripts is a thin wrapper around child_process.exec to easily access node_modules/.bin like in package.json scripts
  await npmRunScripts(
    `cross-env PORT=${PORT} node -r ts-node/register -r …
Run Code Online (Sandbox Code Playgroud)

node.js es6-promise jestjs frisby.js

9
推荐指数
3
解决办法
4796
查看次数

开玩笑:反应路由器测试返回未定义的“参数”

我正在尝试使用MemoryRouterReact-router 文档中指示的具有初始条目的组件来测试组件,以便我可以在包装的组件中包含参数。

组件示例:

render () {
  console.log(this.props.match.params.item)
}
Run Code Online (Sandbox Code Playgroud)

在应用程序中(工作正常):

<Router>
  <Switch>
    <Route exact path='/' render={() => (
      <Redirect to={`/${guessLocale()}`} />
    )} />
    <Route exact path='/:lang' component={Home} />
    <Route exact path='/:lang/:itemName' component={Vidactic} />
  </Switch>
</Router>
Run Code Online (Sandbox Code Playgroud)

在测试中(从酶挂载,匹配未定义):

mount(<MemoryRouter initialEntries={['/fr/pamp']}>
  <Vidactic />
</MemoryRouter>)
Run Code Online (Sandbox Code Playgroud)

所以我使用了这个解决方法,但为什么 initialEntries 本身不起作用?

mount(<MemoryRouter initialEntries={['/fr/pamp']}>
  <Vidactic match={{ params: { item: 'pamp' } }} />
</MemoryRouter>)
Run Code Online (Sandbox Code Playgroud)

reactjs jestjs react-router enzyme react-router-v4

6
推荐指数
1
解决办法
2874
查看次数

在模板中使用动态变量将 nunjucks 与 htmlWebpackPlugin 结合使用

我正在寻找一种使用 Nunjucks 和 htmlWebpackPlugin 在 webpack 编译时生成一些 html 文件的方法。

到目前为止我取得的成就

我设法通过nunjucks-html-loader从 nunjucks 模板文件中实际生成 HTML,但看起来更接近于所述加载器的代码,render 方法被调用,而无需向模板发送变量。

所以,现在使用以下插件配置,我生成了没有动态插入变量的 HTML

new HtmlWebpackPlugin({
  filename: path.join(__dirname, '/' + page.filename),
  template: 'nunjucks-html-loader!assets/templates/' + page.name + '.njk'
})
Run Code Online (Sandbox Code Playgroud)

我试过的

出于测试目的,我尝试对 node_module 本身进行一些更改(我知道,我知道...)并进行了更改

html = template.render(nunjucksContext);

进入

html = template.render(nunjucksContext, { globals: global.globals });

试图global.globals在我的webpack.config.js文件中定义,但这会因以下错误而崩溃

ERROR in Error: Child compilation failed: Module build failed: TypeError: parentFrame.push is not a function

这超出了我的理解。

我想要的是

是使用像 nunjucks 这样的可扩展模板引擎,它允许我像下面这样构建我的模板

<html>
<!-- layout structure inherited from every …
Run Code Online (Sandbox Code Playgroud)

javascript nunjucks webpack html-webpack-plugin

5
推荐指数
1
解决办法
2701
查看次数

React-Router:使用酶在`render`道具内部进行测试

我想测试从/路径到语言环境路径的重定向(例如/en)。因此,该组件的外观如下:

// GuessLocale is imported from a helper
const App = () => (
<Router>
  <Switch>
    <Route exact path='/' render={() => (
      <Redirect to={`/${guessLocale()}`} />
    )} />
    <Route exact path='/:lang' component={Home} />
  </Switch>
</Router>
)
Run Code Online (Sandbox Code Playgroud)

这是当前的测试功能:

it('redirects to a localed path', () => {
  const wrapper = mount(
    <MemoryRouter initialEntries={['/']}>
      <App />
    </MemoryRouter>
  )

  expect(wrapper.find('Redirect')).toHaveLength(1)
})
Run Code Online (Sandbox Code Playgroud)

显然,该测试失败了,因为Redirect组件位于一个子对象中,而该组件作为renderprop 的功能。Route

在测试中,我将App封装在内存路由器中,但是在App组件中,已经存在浏览器路由器,因此我可能需要对其进行重构。

但是即使在Routes组件中分割了路线,我也不知道如何在render道具内部进行测试。

javascript reactjs jestjs react-router enzyme

5
推荐指数
1
解决办法
1367
查看次数

使用带有ramda的fetch api

我正在学习Ramda并试图达到无点编程.为了做到这一点,我尝试在这里和那里进行重构但是却坚持这一点.

我显然认为这不起作用,因为调用是异步的,但我找不到这个代码有什么问题.

// Why is this
const toJSONRamda = R.pipe(
  R.prop('json'), // getting the 'json' function
  R.call // and calling it
)

// different from this
const toJSON = response => response.json()

// Works
fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(toJSON)
  .then(console.log)
  
// Does not Work
fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(toJSONRamda)
  .then(console.log)
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

javascript functional-programming promise ramda.js fetch-api

5
推荐指数
1
解决办法
197
查看次数

ES6 单击按钮时将 div 显示为无

我是 ES6 的新手,我试图在单击内部按钮时不向任何人显示 div。

我尝试了这个,但它不起作用:

const hide = () => {
  const z = document.getElementById('button')
  const y = document.getElementById('block')
  z.onclick = () => {
    y.style.display='none'
  }
}
Run Code Online (Sandbox Code Playgroud)
<div id="block">
  <button id="button" onClick="hide()">my button</button>
</div>
Run Code Online (Sandbox Code Playgroud)

有什么帮助吗?

javascript ecmascript-6

4
推荐指数
1
解决办法
7141
查看次数

React-spring 动画仅在第一次渲染时有效

我尝试为数组中的新条目设置动画,因为它们react-spring在第一次渲染时工作得很好,但在更新时不会设置动画

这是一个代码沙箱,我在其中以间隔重现了该问题:https : //codesandbox.io/s/01672okvpl

import React from "react";
import ReactDOM from "react-dom";
import { Transition, animated, config } from "react-spring";

import "./styles.css";

class App extends React.Component {
  state = { fake: ["a", "b", "c", "d", "e", "f"] };

  fakeUpdates = () => {
    const [head, ...tail] = this.state.fake.reverse();
    this.setState({ fake: [...tail, head].reverse() });
  };

  componentDidMount() {
    setInterval(this.fakeUpdates, 2000);
  }

  componentWillUnmount() {
    clearInterval(this.fakeUpdates);
  }

  render() {
    const { fake } = this.state;
    return (
      <div className="App">
        {fake.map((entry, index) …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-spring

2
推荐指数
1
解决办法
2047
查看次数