小编Flo*_*alk的帖子

如何使用 Jest 测试 Next 中的 _document

我试图在项目中实现 100% 的覆盖率,这是我无法测试的唯一文件,因为我不知道如何做到这一点。

我什至不知道从哪里开始。

我正在使用 Jest 和 React 测试库。该项目使用 NextJS。

import Document from 'next/document'
import { ServerStyleSheet } from 'styled-components'

export default class MyDocument extends Document {
  static async getInitialProps(ctx) {
    const sheet = new ServerStyleSheet()
    const originalRenderPage = ctx.renderPage

    try {
      ctx.renderPage = () =>
        originalRenderPage({
          enhanceApp: App => props => sheet.collectStyles(<App {...props} />),
        })

      const initialProps = await Document.getInitialProps(ctx)
      return {
        ...initialProps,
        styles: (
          <>
            {initialProps.styles}
            {sheet.getStyleElement()}
          </>
        ),
      }
    } finally {
      sheet.seal()
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

ps:我知道覆盖率不是最重要的,但对于这个项目来说,100% …

testing jestjs next.js

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

C#等待使用

我试图使用HtmlAgilityPack.NETCore获取网页将网页加载到程序中。问题在于调试直接从第一个Using语句跳出,因此根本不调用代码和第二个using。没有异常或其他东西被抛出。程序以退出代码0结尾。我在做什么错?我正在使用.net Core 2.2

HttpClient client = new HttpClient();
List<string> htmlContent = new List<string>();
using (var response = await client.GetAsync(URL))
{
    using (var content = response.Content)
    {
        var result = await content.ReadAsStringAsync();
        var document = new HtmlDocument();
        document.LoadHtml(result);
        foreach (HtmlNode node in document.DocumentNode.SelectNodes("//text()"))
        {
            htmlContent.Add(node.InnerText);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# .net-core

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

标签 统计

.net-core ×1

c# ×1

jestjs ×1

next.js ×1

testing ×1